一薄货、注冊蘋果APNS遠(yuǎn)程推送
在didFinishLaunchingWithOptions:中調(diào)用此方法
/**
注冊蘋果推送蹈胡,獲取deviceToken用于推送
@param application application
@param launchOptions application啟動參數(shù)
*/
- (void)registerAPNS:(UIApplication *)application launchOptions:(NSDictionary *)launchOptions
{
CGFloat systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// granted
NSLog(@"User authored notification.");
[application registerForRemoteNotifications];
} else {
// not granted
NSLog(@"User denied notification.");
}
}];
}else if (systemVersion >= 8.0) {
// iOS >= 8 Notifications
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}else {
// iOS < 8 Notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
}
注冊完成回調(diào)方法
/*
* 蘋果推送注冊成功回調(diào)决采,將蘋果返回的deviceToken上傳到服務(wù)器
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
/*
* 蘋果推送注冊失敗回調(diào)
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
二域滥、發(fā)送本地推送的方法:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = @"NotificationTitle";//推送顯示的標(biāo)題
localNotification.fireDate = [NSDate date];//設(shè)置推送時間
localNotification.timeZone = [NSTimeZone defaultTimeZone];//設(shè)置時區(qū)
localNotification.repeatInterval = NSCalendarUnitDay;//設(shè)置重復(fù)的間隔
localNotification.soundName = UILocalNotificationDefaultSoundName;//推送聲音
localNotification.alertBody = @"NotificationContent";//推送內(nèi)容
localNotification.applicationIconBadgeNumber = 1; //顯示在icon上的紅色圈中的數(shù)子
localNotification.userInfo = @{@"content":localNotification.alertBody};//userInfo中的信息
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; //立即發(fā)起本地推送
// UIUserNotificationSettings *localNotificationSettings = [UIUserNotificationSettings settingsForTypes:1 << 2 categories:nil];
// [[UIApplication sharedApplication] registerUserNotificationSettings:localNotificationSettings];
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
三靡挥、收到本地推送通知和遠(yuǎn)程推送通知及點(diǎn)擊通知所走的方法
根據(jù)系統(tǒng)版本分為以下幾個方法序矩,獲取到userInfo之后即可處理相關(guān)信息。
1.Version < iOS10.0
- App已經(jīng)殺死跋破,點(diǎn)擊遠(yuǎn)程和本地推送的消息時調(diào)用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSLog(@"userInfo: %@,",[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
}
- App處于前臺收到遠(yuǎn)程推送消息簸淀,或者后臺(未殺死)點(diǎn)擊遠(yuǎn)程推送消息時調(diào)用
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
NSLog(@"userInfo : %@",userInfo);
if (application.applicationState == UIApplicationStateActive) {
//app在前臺
}else{
//app在后臺點(diǎn)擊遠(yuǎn)程推送
}
}
- App處于前臺收到本地推送消息瓶蝴,或者后臺(未殺死)點(diǎn)擊本地推送消息時調(diào)用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"userInfo : %@",notification.userInfo);
if (application.applicationState == UIApplicationStateActive) {
//app在前臺
}else{
//app在后臺點(diǎn)擊遠(yuǎn)程推送
}
}
2.Version >= iOS10.0
- App已經(jīng)殺死,點(diǎn)擊遠(yuǎn)程和本地推送的消息時調(diào)用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSLog(@"userInfo: %@,",[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
}
UNUserNotificationCenterDelegate 的代理方法
- App處于前臺收到本地推送或者遠(yuǎn)程推送時調(diào)用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0){
NSLog(@"userInfo : %@",notification.request.content.userInfo);
}
- App處于后臺(未殺死)點(diǎn)擊本地推送或者遠(yuǎn)程推送時調(diào)用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED{
NSLog(@"userInfo : %@",notification.request.content.userInfo);
}
PS:應(yīng)該就這些了吧租幕,如有錯誤請留言撒~