推送-个推的使用总结

gt.png

1、注册个推平台账号。 http://www.getui.com/cn/index.html
2、创建推送消息应用,填写应用信息,android填写包名,iOS平台分开发环境和正式环境,可以分别创建应用,上传相应的APNs的p12证书和密码 或者等项目上线时替换成正式环境证书,10分钟生效。
3、项目集成GTSDK,配置AppID、AppSecret、AppKey。
4、在didFinishLaunchingWithOptions中开启个推。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
}

然后注册远程推送,顺序不能反!

//进行用户权限的申请
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
        //在block中会传入布尔值granted,表示用户是否同意
        if (granted) {
            //如果用户权限申请成功,设置通知中心的代理
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });
        }
    }];
}else{
    UIApplication *app = [UIApplication sharedApplication];
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    
    if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        NSLog(@"8.0注册通知");
        [app registerUserNotificationSettings:settings];
    } else {
        NSLog(@"7.0及以下 注册通知");
        [app registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound];
    }
}

5、在注册成功的方法中处理一下苹果返回的deviceToken,去掉两端的尖括号和中间的空格

  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
      NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
      token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
      // 向个推服务器注册deviceToken
      [GeTuiSdk registerDeviceToken:token];
}

6、注册clientId 绑定别名功能:后台可以根据别名进行推送

  - (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
      [GeTuiSdk bindAlias:alias andSequenceNum:clientId];
  }

7、收到远程通知消息

  #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  //  iOS 10: App在前台获取到通知
  - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

      MyLog(@"willPresentNotification:%@", notification.request.content.userInfo);
// 根据APP需要,判断是否要提示用户Badge、Sound、Alert
      completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
  }

  //  iOS 10: 点击通知进入App时触发
  - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {

      MyLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
      // [ GTSdk ]:将收到的APNs信息传给个推统计
      [GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];
      completionHandler(UIBackgroundFetchResultNewData);
  }
  #endif


  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
      // 将收到的APNs信息传给个推统计
      [GeTuiSdk handleRemoteNotification:userInfo];
      completionHandler(UIBackgroundFetchResultNewData);
  }

8、收到透传消息,个推iOS推送,若app在前台运行,消息走透传,若在后台运行,消息走通知,development环境测试推送,只能走透传消息,通知消息仅支持Android。且收到透传消息,没有通知栏提示。

  - (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
         NSDictionary *dict = nil;
          if (payloadData) {
              dict = [NSJSONSerialization JSONObjectWithData:payloadData options:NSJSONReadingAllowFragments error:nil];
              NSLog(@"************接收到透传消息*************************%@",dict);
          }
  }

最后若是收不到消息,问题排查:

(1)检查一下AppId 、AppKey、AppSecret是否配置正确
(2)检查一下deviceToken是否与上传证书的环境一致。


推送-个推的使用总结_第1张图片
检测deviceToken.png

(3)检查一下开启个推和注册远程通知的顺序
(4)注意测试的时候是没有通知栏的,可以在接收透传消息的代理中检测是否接收到推送消息。

======================================================
我是有底线的======================================================

你可能感兴趣的:(推送-个推的使用总结)