您的位置:首页 > 移动开发 > IOS开发

ios集成极光推送的一些坑点及详解

2016-08-12 10:24 387 查看
一、设置badge(角标)

    [[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

    [JPUSHServicesetBadge:0];//清空JPush服务器中存储的badge值
       JPush为每个客户端保存其特定的badge值。客户端有变更时,把badge值更新到JPush服务器。有新的推送时,把这个值+1推送下来。

     (1)+(BOOL)setBadge:(int)value ;     //设置JPush服务器中存储的值

           注:设置badge值,本地仍须调用UIApplication:setApplicationIconBadgeNumber函数

二、推送目标的设备选择方式

       (1)广播

       (2)tag(标签):用标签来进行大规模的设备属性、用户属性的分群。

                                  为安装应用程序的用户打上标签,以便于开发者根据标签来批量下发push消息。

       (3)alias(别名):用别名来标识一个用户,一个设备只能绑定一个别名,但多个设备可以绑定同一个别名。

                                  同一个应用程序内,对不同的用户建议取不同的别名,尽可能根据别名来唯一确定用户。

       (4)RegistrationID(设备标识)

                                集成了Jpush SDK的应用程序在第一次成功注册到JPush服务器时,JPush服务器会给客户端返                   回一个唯一的该设备标识。ios9系统,应用卸载重装,RegistationID会发生改变。

 [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(registerDeviceId)name:kJPFNetworkDidLoginNotificationobject:nil];//登录成功后

    
-(void)registerDeviceId
{

    [JPUSHServiceregistrationID];

    NSLog(@"registrationID:%@",[JPUSHServiceregistrationID]);
    //在登录成功对应的方法中设置标签及别名
   注意:在退出登陆时,要去除对标签及别名的绑定

    /**tags alias

     *空字符串(@“”)表示取消之前的设置

     *nil,此次调用不设置此值

     *每次调用设置有效的别名,覆盖之前的设置

     */

    NSString *alias = @"hello";
    [JPUSHServicesetTags:nilalias:alias
fetchCompletionHandle:^(int iResCode,NSSet *iTags,
NSString *iAlias) {

        NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags , iAlias);//对应的状态码返回为0,代表成功
    }];
}

三、本地通知 (获取apns推送内容)

//1.对于极光推送来说,如果App状态为未运行,函数application:didFinishLaunchingWithOptions:将被调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{

    //本地通知内容获取

    NSDictionary *remoteNotification = [launchOptionsobjectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
   NSLog(@"若launchOptions包含UIApplicationLaunchOptionsRemoteNotificationKey表示用户点击本地通知导致app被启动运行;若不包含则可能为直接点击icon被启动或其他");
}

//2. 如果App状态为正在前台或者点击通知栏的通知消息,苹果的回调函数将被调用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary
*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    //
取得 APNs 标准信息内容
   NSDictionary *aps = [userInfo
valueForKey:@"aps"];
   NSString *content = [aps
valueForKey:@"alert"];
//推送显示的内容
   NSInteger badge = [[aps
valueForKey:@"badge"]
integerValue]; //badge数量
   NSString *sound = [aps
valueForKey:@"sound"];
//播放的声音

    //
取得Extras字段内容
   NSString *customizeField1 = [userInfo
valueForKey:@"customizeExtras"];
//服务端中Extras字段,key是自己定义的

    NSLog(@"content =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content,(long)badge,sound,customizeField1);

    //判断程序是否在前台运行
       if (application.applicationState ==UIApplicationStateActive) {

            //如果应用在前台,在这里执行

            UIAlertView *alertView = [[UIAlertViewalloc]
initWithTitle:@"极光推送"message:content
delegate:nilcancelButtonTitle:@"ok"otherButtonTitles:nil,nil];
            [alertViewshow];
        }

  

    // IOS 7 Support Required,处理收到的APNS信息

    //如果应用在后台,在这里执行

    [JPUSHServicehandleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

    [JPUSHServicesetBadge:0];//清空JPush服务器中存储的badge值

    [application setApplicationIconBadgeNumber:0];//小红点清0操作
}

四、获取自定义消息推送内容

  /**前台运行时,可接收由JPush下发的自定义消息

     *获取自定义消息(只有在前端运行的时候才能收到自定义消息的推送)

     *  kJPFNetworkDidReceiveMessageNotification//
收到消息(非APNS)

     */

    NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];
    [defaultCenteraddObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];
    
- (void)networkDidReceiveMessage:(NSNotification *)notification {
   /**

     *参数描述:

     content:获取推送的内容

     extras:获取用户自定义参数

     customizeField1:根据自定义key获取自定义的value

     */
   NSDictionary * userInfo = [notification
userInfo];
   NSString *content = [userInfo
valueForKey:@"content"];
   NSDictionary *extras = [userInfo
valueForKey:@"extras"];
   NSString *customizeField1 = [extras
valueForKey:@"customizeField1"];
///服务端传递的Extras附加字段,key是自己定义的

    NSInteger badge = [[[userInfovalueForKey:@"aps"]valueForKey:@"badge"]integerValue];
   NSLog(@"%jiaobao--ld",(long)badge);

    NSLog(@"custuserInfo:%@",userInfo);

    NSLog(@"custcontent:%@",content);

    NSLog(@"custextras:%@",extras);
   NSLog(@"customizeField1:%@",customizeField1);

    NSLog(@"cust获取注册ID:%@",    [JPUSHServiceregistrationID]);

    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: