iOS p8证书创建注册验证

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



1. 创建证书

  • 按下图创建证书然后下载, 就是.p8文件, 给后台人员, 此处OK.
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). 点击发送。成功与否就能看到了.
image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,314评论 19 139
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 6,854评论 10 16
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 30,104评论 8 265
  • 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知...
    莫离_焱阅读 6,684评论 1 8
  • 182114_265阅读 153评论 0 0

友情链接更多精彩内容