关闭

JPush自定义消息的集成

标签: ios第三方使用
151人阅读 评论(0) 收藏 举报
分类:

注意:只有在前端运行的时候才能收到自定义消息的推送

1,实现方法
获取iOS的推送内容需要在delegate类中注册通知并实现回调方法。在方法
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary ) launchOptions 加入下面的代码:

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

实现回调方法 networkDidReceiveMessage

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

参数描述:
content:获取推送的内容
extras:获取用户自定义参数
customizeField1:根据自定义key获取自定义的value
更多实现参考 SDK下载压缩包中的 demo。

2,在某个VC中实现接受自定义消息
头文件需要引入#import “JPUSHService.h”和#import “AppDelegate.h”

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

在-dealloc方法中移除通知

  [defaultCenter removeObserver:self                       name:kJPFNetworkDidReceiveMessageNotification
                         object:nil];

实现通知方法

- (void)networkDidReceiveMessage:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  NSString *title = [userInfo valueForKey:@"title"];
  NSString *content = [userInfo valueForKey:@"content"];
  NSDictionary *extra = [userInfo valueForKey:@"extras"];
 //之后解析,根据需求添加自定义方法

3,接到通知之后跳转界面

/** APP已经接收到“远程”通知(推送)   */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    NSLog(@"\n>>>[Receive RemoteNotification - Background Fetch]:%@\n\n",userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
    self.callid = nil;
    NSString *userdata = [userInfo objectForKey:@"c"];
    NSLog(@"远程推送userdata:%@",userdata);
    if (userdata) {
        NSDictionary*callidobj = [NSJSONSerialization JSONObjectWithData:[userdata dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"远程推送callidobj:%@",callidobj);
        if ([callidobj isKindOfClass:[NSDictionary class]]) {
            self.callid = [callidobj objectForKey:@"callid"];
        }
    }
    //JPUSH
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"iOS7及以上系统,收到通知:%@", [self logDic:userInfo]);
    NSString *string=[userInfo objectForKey:@"strJpushType"];
    //跳转到头条(例)
    if ([string integerValue] ==1) {
        YMToutiaoViewController *vc = [[YMToutiaoViewController alloc] init];
        vc.lineId=userInfo[@"strJPushId"];
        vc.lineName=userInfo[@"strJpushTitle"];
        vc.isPush=@"1";
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
        [self.window.rootViewController presentViewController:nav animated:YES completion:^{
        }];
    }

    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 || application.applicationState>0) {
    //某一界面添加通知
        //        [rootViewController addNotificationCount];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27166次
    • 积分:1192
    • 等级:
    • 排名:千里之外
    • 原创:93篇
    • 转载:8篇
    • 译文:0篇
    • 评论:13条
    最新评论