[那些年接过的第三方sdk]apns推送 cocos ios

1、首先导入框架 <UserNotifications/UserNotifications.h>


WechatIMG1.png

2、打开工程Target中的Push Notifications,不然会获取不到deviceToken,AppController.mm文件里注册推送功能.


WX20191112-112347@2x.png
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      
     if ([UIDevice currentDevice].systemVersion.doubleValue <= 8.0) {

         /**
            向服务器发请求,要注册推送功能,以此获取到服务器返回的deviceToken
            type 用来说明 支持的通知形式
            如 横幅 声音  角标
          */
          [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
         NSLog(@"注册推送功能,以此获取到服务器返回的deviceToken");
         
     }else{

          if (@available(iOS 10.0, *)) {
               //iOS10
               // 1.向用户请求可以给用户推送消息
               UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
               center.delegate = self;
               [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
                   if (!error)
                      {
                          NSLog(@"request authorization succeeded!");
                      }else{
                          NSLog(@"%s", error);
                          
                      }
                   
               }];
          }else{

               // 1.向用户请求可以给用户推送消息
               UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
               [application registerUserNotificationSettings:notificationSettings];

          }
          NSLog(@"注册远程通知(拿到用户的DeviceToken)");
      // 2.注册远程通知(拿到用户的DeviceToken)
          [application registerForRemoteNotifications];
      }

    return YES;
}

3、获得DeviceToken,监听通知

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    NSLog(@"%s", __func__);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
    NSLog(@"%s", __func__);
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
    //保存deviceToken
    NSString *token=[self HexStringWithData:pToken];
    NSLog(@"regisger success:%@",token);
}
-(NSString *)HexStringWithData:(NSData *)data{
    Byte *bytes = (Byte *)[data bytes];
    NSString *hexStr=@"";
    for(int i=0;i<[data length];i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1){
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        }
        else{
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
        }
    }
    hexStr = [hexStr uppercaseString];
    return hexStr;
}

4、用smartpush测试功能,git地址https://github.com/shaojiankui/SmartPush,关于测试工具用到的cer证书文件,可以参照这个链接,就是太老了,有些对不上.http://www.cocoachina.com/articles/26482
小红点相关知识
https://www.jianshu.com/p/ad0e2d34ccff

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。