iOS10下的推送增加了一些新的東西,廢話不多說直接上代碼
1.首先肯定是要注冊通知,由于各個系統的注冊通知方法有不同,所以注冊也是要多做些判斷,兼容更多系統
/** 注冊遠程通知 */
- (void)registerRemoteNotification {
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 10.0) {
#if __ipHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8編譯會調用
UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter];
center.delegate =self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |UNAuthorizationOptionSound |UNAuthorizationOptionAlert |UNAuthorizationOptionCarPlay)completionHandler:^(BOOL granted,NSError *_Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[[UIapplicationsharedApplication] registerForRemoteNotifications];
#else // Xcode 7編譯會調用
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
} elseif ([[[UIDevicecurrentDevice] systemVersion]floatValue] >= 8.0) {
UIUserNotificationType types = (UIUserNotificationTypeAlert |UIUserNotificationTypeSound |UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes:typescategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:settings];
[[UIApplicationsharedApplication] registerForRemoteNotifications];
} else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound |UIRemoteNotificationTypeBadge);
[[UIApplicationsharedApplication] registerForRemoteNotificationTypes:apn_type];
}
}
注意!!!!!:xcode8需要打開通知開關
按照上圖所示
2.上一步是注冊通知,下面就是接受注冊成功的deviceToken了,這里的deviceToken是NSData,所以要拿到字符串要做一些處理
// 注冊成功的代理方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceTokendescription] stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" "withString:@""];
NSLog(@"/nDeviceToken:%@/n/n", token);
}
// 注冊失敗的代理方法- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"/n>>>[DeviceToken Error]:%@/n/n", error.description);
}
3.注冊是搞定了,下面就要處理通知了。在處理通知方面,iOS10又增加了新的方法,不過老方法還是會調用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// [ GTSdk ]:將收到的APNs信息傳給個推統計
[GeTuiSdkhandleRemoteNotification:userInfo];
//應用的當前狀態判斷
if(application.applicationState ==UIApplicationStateInactive){
//當你的應用在后臺時,當你點擊通知欄消息時,會進入此條件
}
// 控制臺打印接收APNs信息
HWLog(@"/n>>>[Receive RemoteNotification:%@/n/n", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
注意:這個方法里最好加上應用狀態的判斷,這個枚舉值有三個
typedefNS_ENUM(NSInteger, UIApplicationState) {
UIApplicationStateActive, //應用激活狀態
UIApplicationStateInactive,//應用待激活狀態或將要激活
UIApplicationStateBackground//應用在后臺
} NS_ENUM_AVAILABLE_IOS(4_0);
下面是iOS10新增加的兩個方法(這兩個方法就夠用)#PRagma mark - iOS 10中收到推送消息
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// iOS 10: App在前臺獲取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
HWLog(@"willPresentNotification:%@", notification.request.content.userInfo);
// 根據APP需要,判斷是否要提示用戶Badge、Sound、Alert
completionHandler(UNNotificationPresentationOptionBadge |UNNotificationPresentationOptionSound |UNNotificationPresentationOptionAlert);
}
// iOS 10: 點擊通知進入App時觸發
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
HWLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
completionHandler();
}
#endif
其中,在第二個方法里做事就可以了,這個方法可以拿到APNS的數據,只是自己做好了狀態判斷,這些方法都是需要點擊通知欄進入app才會調用新聞熱點
疑難解答