推送配置的那些證書等我就不做解釋蚊荣,iOS以后系統(tǒng)會(huì)自動(dò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 注冊(cè)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è)勾選上