iOS中提供了2種推送通知
- 本地推送通知(Local Notification)
- 遠(yuǎn)程推送通知(Remote Notification)
推送通知的作用
- 可以讓不在前臺(tái)運(yùn)行的app铅辞,告知用戶app內(nèi)部發(fā)生了什么事情
- 推送通知的呈現(xiàn)效果總結(jié)
- 用戶接收的推送通知厌漂,都會(huì)展示在“通知中心”
- 從屏幕頂部往下滑,就能調(diào)出“通知中心”
- 顯示橫幅還是UIAlertView斟珊,取決于用戶的設(shè)置
總結(jié)一下苇倡,推送通知有5種不同的呈現(xiàn)效果
- 在屏幕頂部顯示一塊橫幅(顯示具體內(nèi)容)
- 在屏幕中間彈出一個(gè)UIAlertView(顯示具體內(nèi)容)
- 在鎖屏界面顯示一塊橫幅(鎖屏狀態(tài)下,顯示具體內(nèi)容)
- 更新app圖標(biāo)的數(shù)字(說(shuō)明新內(nèi)容的數(shù)量)
- 播放音效(提醒作用)
** 推送通知的使用細(xì)節(jié)**
- 發(fā)出推送通知時(shí)囤踩,如果當(dāng)前程序正運(yùn)行在前臺(tái)旨椒,那么推送通知就不會(huì)被呈現(xiàn)出來(lái)
- 點(diǎn)擊推送通知后,默認(rèn)會(huì)自動(dòng)打開發(fā)出推送通知的app
- 不管app打開還是關(guān)閉堵漱,推送通知都能如期發(fā)出
本地推送
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* UIUserNotificationTypeNone = 0, 無(wú)類型(不給用戶發(fā)通知)
UIUserNotificationTypeBadge = 1 << 0, 是否可以改變應(yīng)用圖標(biāo)右上角的提示數(shù)字
UIUserNotificationTypeSound = 1 << 1, 該通知是否會(huì)有聲音
UIUserNotificationTypeAlert = 1 << 2, 是否有彈出提示
*/
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
// 跳轉(zhuǎn) UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 300, 300, 300);
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", launchOptions];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
/**
* 點(diǎn)擊通知打開應(yīng)用的時(shí)候會(huì)執(zhí)行該方法
* 應(yīng)用在前臺(tái)的時(shí)候,收到通知也會(huì)執(zhí)行該方法
*
* @param notification 通知
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 300, 300);
label.backgroundColor = [UIColor redColor];
label.text = [NSString stringWithFormat:@"%@", notification]
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.window.rootViewController.view addSubview:label];
// if (application.applicationState == UIApplicationStateBackground) {
//
// }
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return YES;
}
@end
推送
#import "ViewController.h"
@interface ViewController ()
/**
* 點(diǎn)擊按鈕后添加本地通知
*/
- (IBAction)addLocalNote;
/**
* 移除通知(不常用)
*/
- (IBAction)removeLocalNote;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/**
* 點(diǎn)擊按鈕后添加本地通知
*/
- (IBAction)addLocalNote {
/*
@property(nonatomic,copy) NSDate *fireDate;
@property(nonatomic,copy) NSTimeZone *timeZone;
@property(nonatomic) NSCalendarUnit repeatInterval;
@property(nonatomic,copy) NSCalendar *repeatCalendar;
@property(nonatomic,copy) CLRegion *region NS_AVAILABLE_IOS(8_0);
@property(nonatomic,assign) BOOL regionTriggersOnce NS_AVAILABLE_IOS(8_0);
@property(nonatomic,copy) NSString *alertBody;
@property(nonatomic) BOOL hasAction;
@property(nonatomic,copy) NSString *alertAction;
@property(nonatomic,copy) NSString *alertLaunchImage;
@property(nonatomic,copy) NSString *soundName;
UILocalNotificationDefaultSoundName @property(nonatomic) NSInteger applicationIconBadgeNumber;
@property(nonatomic,copy) NSDictionary *userInfo;
*/
// 1.創(chuàng)建一個(gè)本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.設(shè)置通知發(fā)出的時(shí)間
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// 1.2.設(shè)置通知發(fā)出的內(nèi)容
localNote.alertBody = @"吃飯了嗎?";
// 1.3.是否彈出提示框
localNote.hasAction = YES;
// 1.4.設(shè)置提示框
localNote.alertAction = @"趕緊查看";
// 1.5.設(shè)置啟動(dòng)的圖片
localNote.alertLaunchImage = @"1111";
// 1.6.設(shè)置啟動(dòng)的音效
localNote.soundName = UILocalNotificationDefaultSoundName;
// 1.7.設(shè)置應(yīng)用圖標(biāo)提醒的數(shù)字
localNote.applicationIconBadgeNumber = 999;
// 1.8.如果想將通知的信息傳遞過(guò)去,必須使用userInfo屬性
localNote.userInfo = @{@"msg" : @"吃飯了嗎", @"date" : localNote.fireDate};
// 2.調(diào)度通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (IBAction)removeLocalNote {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// [UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)
}
@end
遠(yuǎn)程推送
** AppDelegate.m**
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
// 1.向用戶請(qǐng)求可以給用戶推送消息
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[application registerUserNotificationSettings:settings];
// 2.注冊(cè)遠(yuǎn)程通知(拿到用戶的DeviceToken)
[application registerForRemoteNotifications];
} else {
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
// 頁(yè)面的跳轉(zhuǎn)
}
[application setApplicationIconBadgeNumber:0];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// 將用戶的用戶名和deviceToken發(fā)送給服務(wù)器,讓服務(wù)器進(jìn)行保存?zhèn)浞菁纯? NSLog(@"%@", deviceToken);
}
/**
* 當(dāng)接受到遠(yuǎn)程通知的時(shí)候會(huì)調(diào)用該方法
*
* @param userInfo 遠(yuǎn)程通知的信息
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 在這里可以跳轉(zhuǎn)的其他頁(yè)面
NSLog(@"%@", userInfo);
}
/**
* 如果接受到遠(yuǎn)程通知時(shí),想要后臺(tái)執(zhí)行任務(wù),則實(shí)現(xiàn)調(diào)用該方法
*
* @param userInfo
* @param completionHandler 后臺(tái)執(zhí)行完之后要告知系統(tǒng),是否更新成功
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
@end