iOS p8证书创建注册验证

测试了p8证书实用. 本文介绍路线: 创建p8证书----- 注册推送 --- 获取token --- 测试推送是否成功



1. 创建证书

  • 按下图创建证书然后下载, 就是.p8文件, 给后台人员, 此处OK.
iOS p8证书创建注册验证_第1张图片
image.png

2. APP注册推送

  • a). 在AppDelegate.m文件添加方法
#pragma mark 远程推送
- (void) register_push_with_application:(UIApplication *)application launchOptions:(NSDictionary *)launchOptions {
    
    if ([UIDevice currentDevice].systemVersion.doubleValue >= 10.0) {
        
        UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
        
        [center setDelegate:self];
        
        UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
        
        [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (granted) {
                
                NSLog(@"注册成功");
                
            }else{
                
                NSLog(@"注册失败");
                
            }
            
        }];
        
    }else if ( [UIDevice currentDevice].systemVersion.doubleValue >= 8.0){
        
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge |
        
        UIUserNotificationTypeSound |
        
        UIUserNotificationTypeAlert;
        
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        
        [application registerUserNotificationSettings:settings];
        
    }
    
    // 注册获得device Token
    
    [application registerForRemoteNotifications];
}
  • b). 在方法:didFinishLaunchingWithOptions 调用

[self register_push_with_application:application launchOptions:launchOptions];

3. APP获取token

  • 在方法 application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 获取token 发送给后台.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    //保存 deviceToken
    NSString *str = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    NSString *deviceTokenStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"token:%@, 发送token给后端", deviceTokenStr);
}
  • 到这就完成了基本的推送功能.

4. 测试推送是否成功

测试一下是否成功。

    1. 运行你的项目(注意: 真机运行), 这样就能拿到token, 保存你拿到的token后面有用.
    1. 这是一个测试推送的软件大家从github下载下来,直接运行。
      • a). 把你的证书路径、token以及要推送的消息放到指定的地方。
      • b). 点击连接服务器,这个时候会有访问钥匙串的请求,允许访问钥匙串。
      • c). 点击发送。成功与否就能看到了.
iOS p8证书创建注册验证_第2张图片
image.png

你可能感兴趣的:(iOS p8证书创建注册验证)