极光推送(接收后台消息和自定义消息)

1.极光推送包括:APNs推送,JPush应用内消息。

  JPush 代理开发者的应用(需要基于开发者提供的应用证书),向苹果 APNs 服务器推送。由 APNs Server 推送到 iOS 设备上。

 JPush 应用内推送部分,即 App 启动时,内嵌的 JPush SDK 会开启长连接到 JPush Server,从而 JPush Server 可以推送消息到 App 里。

2.根据极光文档中JPush集成指南配置证书,导入框架,配置工程。

3.在appDelegate的didfinishlaunching方法里添加

    //注册极光推送

[JPUSHService setupWithOption:nil appKey:@"" channel:nil apsForProduction:YES];

   //获取自定义消息

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

    

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

        //可以添加自定义categories

        [JPUSHService registerForRemoteNotificationTypes:(

                    UIUserNotificationTypeBadge |

                    UIUserNotificationTypeSound |

                    UIUserNotificationTypeAlert) categories:nil];

    } else {

        //categories 必须为nil

        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

             UIRemoteNotificationTypeSound |

             UIRemoteNotificationTypeAlert) categories:nil];

    }

#pragma mark 获取自定义消息内容

- (void)networkDidReceiveMessage:(NSNotification *)notification {

    NSDictionary * userInfo = [notification userInfo];

    NSString *content = [userInfo valueForKey:@"content"];

    NSDictionary *extras = [userInfo valueForKey:@"extras"];

    NSString *customizeField1 = [extras valueForKey:@"123456"]; //自定义参数,key是自己定义的

    NSLog(@"自定义message:%@",userInfo);

    NSLog(@"%@",content);

    NSLog(@"%@",extras);

    NSLog(@"%@",customizeField1);

}

#pragma mark 接收推送的消息

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{

    NSLog(@"推送的消息:NSDictionary:%@",userInfo);

    [JPUSHService handleRemoteNotification:userInfo];

    completionHandler(UIBackgroundFetchResultNewData);

}


#pragma mark 极光推送添加的内容

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

     [JPUSHService registerDeviceToken:deviceToken];

}

#pragma mark 失败回调方法

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error

{

    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}


#pragma mark 极光推送设置别名和标签,后台可以根据别名和签名给指定的用户发送消息

温馨提示:设置别名和签名时注意call back的处理结果,返回值为0才设置成功

        if ([dic[@"weProtectUserType"] isEqualToString:@"administrator"]) {

             NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"A", nil];

            [JPUSHService setTags:set alias:alisa fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {

                NSLog(@"设置签名与别名1:%d",iResCode);

                NSLog(@"推送返回的%@%@",iTags,iAlias);

            } ];

        }else

        {

             NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"C", nil];

            [JPUSHService setTags:set alias:alisa fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {

                NSLog(@"设置签名与别名2:%d",iResCode);

                NSLog(@"推送返回的%@%@",iTags,iAlias);

            } ];

        }



                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值