iOS10推送遇到的坑

推送配置的那些證書等我就不做解釋,iOS以后系統(tǒng)會自動配置你做好的證書,xcode-->Genera


在AppDelegate.h中的didFinishLaunchingWithOptions書寫

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

//iOS10特有

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

// 必須寫代理,不然無法監(jiān)聽通知的接收與點(diǎn)擊

center.delegate = self;

[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (granted) {

// 點(diǎn)擊允許

[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

[[UIApplication sharedApplication] registerForRemoteNotifications];

}];

}

}];

}else if (version >= 8.0) {

//環(huán)信推送設(shè)置

//iOS8 注冊APNS

if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {

[application registerForRemoteNotifications];

UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |

UIUserNotificationTypeSound |

UIUserNotificationTypeAlert;

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];

[application registerUserNotificationSettings:settings];

}

else{

UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeAlert;

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];

}



接下來就是在AppDelegate.h遵守iOS10新出來的UNUserNotificationCenterDelegate。

從服務(wù)器獲取deviceToken并傳給給SDK

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{}

我這邊是環(huán)信的SDK



接下來就是實(shí)現(xiàn)UNUserNotificationCenterDelegate的協(xié)議了

實(shí)現(xiàn)方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler



實(shí)現(xiàn)了UNUserNotificationCenterDelegate協(xié)議之后我們就要把相應(yīng)的push開關(guān)打開,這是非常關(guān)鍵的

前往 xcode -->TARGETS-->Capabilities 把Push Notification開關(guān)打開



別以為把這打開就夠l,那你就錯(cuò)了,我一開始也這么認(rèn)為但怎么也收不到推送,原來還有一個(gè)是要勾選的

也是在xcode -->TARGETS-->Capabilities

并把最后一個(gè)勾選上


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容