今天最近做了個(gè)遠(yuǎn)程推送功能古掏,未使用第三方開(kāi)發(fā)。發(fā)現(xiàn)一個(gè)問(wèn)題:在保證證書(shū)正確丧枪、網(wǎng)絡(luò)連接正常庞萍、首次進(jìn)入允許通知的情況下拧烦,獲取不到APNs的deviceTonken回調(diào)钝计。
既不調(diào)用application:didRegisterForRemoteNotificationsWithDeviceToken;方法
也不調(diào)用application:didFailToRegisterForRemoteNotificationsWithError:方法齐佳。
網(wǎng)上搜了不少資料债沮,結(jié)果大家給的解決方案幾乎都一致,把原因引向了iOS8.0+和iOS7系統(tǒng)問(wèn)題疫衩,并給出根據(jù)不同系統(tǒng)版本對(duì)remotNotifi進(jìn)行注冊(cè)的方法:
iOS8.0+ registerUserNotificationSettings:
iOS8.0- registerForRemoteNotificationTypes:
(還有人說(shuō)自己也不知道怎么了,后來(lái)就突然好了L嵴搿2芸!皆愉!)
找了很久很久很久...,都特么都是這個(gè)方法幕庐。
我想說(shuō):這特么誰(shuí)不知道。
被逼無(wú)奈瑟由,我只能一步一步的從申請(qǐng)證書(shū)到代碼編寫(xiě)一步一步的檢查冤寿,一步一步的看...還是沒(méi)找到原因。我累個(gè)艸了督怜。
沒(méi)辦法了,只能從appdelegate代理方法入手号杠,找原因和解決方法。
原因是沒(méi)找到屉凯,但是解決方法卻找到了,直接貼代碼:
// This callback will be made upon calling -[UIApplication registerUserNotificationSettings:]. The settings the user has granted to the application will be passed in as the second argument.
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
iOS8.0以后可以用這個(gè)作為second argument悠砚。然后:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
果斷解決了偷仿,什么deviceToken問(wèn)題都沒(méi)有了。
一個(gè)推送做的我心累酝静,雖然我不知道什么原因?qū)е碌墨@取不到deviceToken,但是問(wèn)題總算解決了宗苍。
代碼:
//remote 授權(quán)
- (void)registRemoteNotification{
#ifdef __IPHONE_8_0
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
#else
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
}
#pragma mark - remote Notification
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken{
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"device token is %@",token);
[[NSUserDefaults standardUserDefaults] setValue:token forKey:@"video_deviceToken"];
}
- (void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"%@",error);
}
//ios 7.0
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"did recevive remote noti userInfo %@ for ios 7 ",userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
/**
*? 系統(tǒng)會(huì)估量App消耗的電量讳窟,并根據(jù)傳遞的UIBackgroundFetchResult 參數(shù)記錄新數(shù)據(jù)是否可用
*? 調(diào)用完成的處理代碼時(shí)敞恋,應(yīng)用的界面縮略圖會(huì)自動(dòng)更新
*/
NSLog(@"did Receive Remote Notification userInfo %@",userInfo);
switch (application.applicationState) {
case UIApplicationStateActive:
completionHandler(UIBackgroundFetchResultNewData);
break;
case UIApplicationStateInactive:
completionHandler(UIBackgroundFetchResultNewData);
break;
case UIApplicationStateBackground:
completionHandler(UIBackgroundFetchResultNewData);
break;
default:
break;
}
}