推送配置的那些证书等我就不做解释,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
并把最后一个勾选上