一、關(guān)于推送
1、本地推送
只要用戶允許了推送通知,并且通知提醒類型不為空,就可以進(jìn)行本地推送。
2、遠(yuǎn)程推送
首先需要用戶允許了推送通知,并且通知提醒類型不為空;其次需要注冊(cè)遠(yuǎn)程推送,并將deviceToken上傳給推送服務(wù)器,服務(wù)器配置好推送證書,App項(xiàng)目配置好推送權(quán)限文件,這樣服務(wù)器才能推送消息到App。
二、iOS8~iOS10系統(tǒng)推送
- App位于后臺(tái)或App未運(yùn)行時(shí),才能顯示接收到的推送消息,如橫幅提示、聲音提示;App位于前臺(tái)時(shí)不會(huì)顯示接收到的推送消息。
- 推送消息一旦發(fā)出,不能修改消息內(nèi)容。
- 本地推送數(shù)量上限為64個(gè)。
- 可以指定用戶動(dòng)作Actions,顯示消息可以指定顯示按鈕、輸入框。
1、本地推送
(1)注冊(cè)推送
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
//要定制用戶Action的話,可以設(shè)置categories
//注冊(cè),會(huì)觸發(fā)權(quán)限請(qǐng)求
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
//注冊(cè)完成代理方法
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {}
(2)創(chuàng)建通知
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = @"測(cè)試本地推送";
notification.applicationIconBadgeNumber = 66;
notification.userInfo = @{@"testdata":@"xxxxxxx"};
//定時(shí)推送
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];//5秒后推送
//定點(diǎn)推送,每次進(jìn)入或離開指定位置區(qū)域時(shí)發(fā)送通知
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(30.5516493549,114.3165377299);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:50 identifier:@"泛悅中心"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
notification.region = region;
notification.regionTriggersOnce = NO;
//定期推送,立即觸發(fā)通知,之后每天重復(fù)通知
notification.fireDate = nil;
notification.repeatCalendar = [NSCalendar currentCalendar];
notification.repeatInterval = NSCalendarUnitDay;
(3)推送通知
//按計(jì)劃推送本地通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
//立即推送本地通知
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
(4)取消推送
[[UIApplication sharedApplication] cancelLocalNotification:notification];
(5)接收并處理推送
//App啟動(dòng)時(shí)處理本地推送消息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {}
return YES;
}
//點(diǎn)擊本地通知提示時(shí),或App位于前臺(tái)時(shí),處理推送消息
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {}
(6)處理用戶Action
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
NSLog(@"iOS8 處理用戶定制的Actions:%@", notification.userInfo);
//處理完畢必須調(diào)用completionHandler
completionHandler();
}
2、遠(yuǎn)程推送
(1)注冊(cè)推送
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
//觸發(fā)權(quán)限請(qǐng)求
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
//注冊(cè)遠(yuǎn)程推送
[[UIApplication sharedApplication] registerForRemoteNotifications];
(2)上傳設(shè)備Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"iOS8 遠(yuǎn)程推送注冊(cè)完成,token:%@", deviceToken);
//TODO: 將token上傳到自己的推送服務(wù)器上
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"iOS8 遠(yuǎn)程推送注冊(cè)失敗,error:%@", error);
}
(3)接收與處理推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"iOS10以下系統(tǒng)點(diǎn)擊通知消息或App位于前臺(tái)收到推送通知消息時(shí)執(zhí)行該方法");
}
//如果實(shí)現(xiàn)了該方法,同時(shí)需要配置plist推送通知后臺(tái)執(zhí)行權(quán)限,同時(shí)上面的方法不再被調(diào)用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"iOS10以下系統(tǒng)點(diǎn)擊通知消息或App位于前臺(tái)收到推送通知消息時(shí)執(zhí)行該方法");
}
三、iOS10及以上系統(tǒng)推送
- 推送內(nèi)容更加豐富,可以設(shè)置title、subtitle、body、圖片、音頻、視頻等附件
- 推送通知管理更好,可以查看、更新、刪除通知
- 應(yīng)用位于前臺(tái)時(shí)也可以展示通知
1、本地推送
(1)注冊(cè)推送
//需要引入系統(tǒng)頭文件
#import <UserNotifications/UserNotifications.h>
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions options = UNAuthorizationOptionBadge + UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
//注冊(cè)推送,同時(shí)請(qǐng)求系統(tǒng)權(quán)限
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"granted:%@, error:%@", @(granted), error);
}];
(2)創(chuàng)建通知
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"測(cè)試iOS10定時(shí)推送本地通知";
content.subtitle = @"subTitle";
content.badge = @33;
content.body = @"test body";
content.sound = [UNNotificationSound defaultSound];
content.userInfo = @{@"testdata":@"xxxxx"};
//定時(shí)推送
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
UNNotificationRequest *request =[UNNotificationRequest requestWithIdentifier:@"testID" content:content trigger:trigger];
//定期推送
NSDateComponents *components = [NSDateComponents new];
components.weekday = 4;//每周三
components.hour = 20;//晚上8點(diǎn)
components.minute = 1;//1分
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
UNNotificationRequest *request =[UNNotificationRequest requestWithIdentifier:@"testID2" content:content trigger:trigger];
//定點(diǎn)推送
CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(30.5516493549,114.3165377299);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:centerCoordinate radius:50 identifier:@"泛悅中心"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
UNLocationNotificationTrigger *trigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:YES];
UNNotificationRequest *request =[UNNotificationRequest requestWithIdentifier:@"testID3" content:content trigger:trigger];
(3)推送通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"添加推送成功");
}];
(4)接收并處理通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSLog(@"iOS10 App位于前臺(tái)時(shí),接收到本地、遠(yuǎn)程推送時(shí)的處理");
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {} //遠(yuǎn)程推送
else {} //本地推送
UNNotificationPresentationOptions options = UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound;
completionHandler(options);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSLog(@"iOS10 點(diǎn)擊推送消息時(shí)處理推送消息");
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {} //遠(yuǎn)程推送
else {} //本地推送
completionHandler();
}
2、遠(yuǎn)程推送
(1)注冊(cè)
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;//設(shè)置該代理對(duì)象盡量在willFinishLaunchingWithOptions方法中,設(shè)置太晚可能無法及時(shí)處理通知消息
UNAuthorizationOptions options = UNAuthorizationOptionBadge + UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
//請(qǐng)求指定推送設(shè)置的權(quán)限
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
NSLog(@"granted:%@, error:%@", @(granted), error);
//獲得權(quán)限后注冊(cè)遠(yuǎn)程推送通知
if (!error && granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
(2)上傳設(shè)備Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"iOS10 遠(yuǎn)程推送注冊(cè)完成,token:%@", deviceToken);
//TODO: 將token上傳到自己的推送服務(wù)器上
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"iOS10 遠(yuǎn)程推送注冊(cè)失敗,error:%@", error);
}
(4)接收并處理通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSLog(@"iOS10 App位于前臺(tái)時(shí),接收到本地、遠(yuǎn)程推送時(shí)的處理");
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {} //遠(yuǎn)程推送
else {} //本地推送
UNNotificationPresentationOptions options = UNNotificationPresentationOptionAlert + UNNotificationPresentationOptionBadge + UNNotificationPresentationOptionSound;
completionHandler(options);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
NSLog(@"iOS10 點(diǎn)擊推送消息時(shí)處理推送消息");
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {} //遠(yuǎn)程推送
else {} //本地推送
completionHandler();
}