iOS10推送遇到的坑

推送配置的那些证书等我就不做解释,iOS以后系统会自动配置你做好的证书,xcode-->Genera


在AppDelegate.h中的didFinishLaunchingWithOptions书写

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

//iOS10特有

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

// 必须写代理,不然无法监听通知的接收与点击

center.delegate = self;

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

if (granted) {

// 点击允许

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

[[UIApplication sharedApplication] registerForRemoteNotifications];

}];

}

}];

}else if (version >= 8.0) {

//环信推送设置

//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。

从服务器获取deviceToken并传给给SDK

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

{}

我这边是环信的SDK



接下来就是实现UNUserNotificationCenterDelegate的协议了

实现方法

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



实现了UNUserNotificationCenterDelegate协议之后我们就要把相应的push开关打开,这是非常关键的

前往 xcode -->TARGETS-->Capabilities 把Push Notification开关打开



别以为把这打开就够l,那你就错了,我一开始也这么认为但怎么也收不到推送,原来还有一个是要勾选的

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

并把最后一个勾选上


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容