iOS 10 Push Notifications 消息推送配置

iOS 10的消息通知和之前有很大变化,不过好消息是它变得配置更加简单更加集中了。

1.登陆开发者账号,这个界面排版比以前好看了一些,点击中间的证书。

iOS 10 Push Notifications 消息推送配置_第1张图片

2.接下来点选 App IDs

iOS 10 Push Notifications 消息推送配置_第2张图片

3.在这里可以看到各种权限的开启状态,这个我已经配置好了。

iOS 10 Push Notifications 消息推送配置_第3张图片

4.点击编辑,配置开发模式和生产模式的证书,配置好之后可以下载下来双击安装到本地。

iOS 10 Push Notifications 消息推送配置_第4张图片

这里要上传一个本地证书,生成方式如下:
1.钥匙串访问 -> 证书助理 -> 从证书颁发机构请求证书…


iOS 10 Push Notifications 消息推送配置_第5张图片

2.邮箱随便填写,一定要存在本地磁盘你能找到的地方


iOS 10 Push Notifications 消息推送配置_第6张图片

5.到这里还没完,还需要打开我们的工程开启通知开关,如图。

iOS 10 Push Notifications 消息推送配置_第7张图片

6.这样提交新的版本之后就可以顺利收到通知啦,由于历史原因我们用的是某萌,我也不想啦。还需要把之前的 apns 证书打包成 .p12 文件传递到它们的后台。

简单讲一下如何导出.p12文件:
在自带 app 中找到钥匙串,右键你要导出的证书,就是这个样子。


iOS 10 Push Notifications 消息推送配置_第8张图片

设密码,导出


iOS 10 Push Notifications 消息推送配置_第9张图片

7.如果你也用某萌,代码示例

注册通知

- (void)registerAPNS {
    if (SYSTEM_VER >= 10.0f) {// 下一个版本就只支持10以上了
        // iOS10特有
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        // 必须写代理,不然无法监听通知的接收与点击
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert |
                                                 UNAuthorizationOptionBadge |
                                                 UNAuthorizationOptionSound)
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (granted) {
                                      // 点击允许
                                      NSLog(@"注册APNS成功");
                                      [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                                          NSLog(@"%@", settings);
                                      }];
                                  } else {
                                      // 点击不允许
                                      NSLog(@"注册APNS失败");
                                  }
                              }];
    }else {
        // iOS8 - iOS9
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
                                                                             UIUserNotificationTypeSound |
                                                                             UIUserNotificationTypeBadge
                                                                                                              categories:nil]];
    }
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

回调

// 获得Device Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *strDT = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
                        stringByReplacingOccurrencesOfString:@">" withString:@""]
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
    
    NSString *userDefToken = @"你的 token";
    do something...
}

// 获得Device Token失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
#warning 配置 aps-environment
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

// iOS 7~9收到通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSLog(@"iOS7及以上系统,收到通知:%@", userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}

你可能感兴趣的:(iOS 10 Push Notifications 消息推送配置)