iOS集成极光推送 通知 和 自定义消息

支持的版本

r1.2.5 以后。

功能说明

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

从jpush服务器获取用户推送的自定义消息的内容、标题、附件字段等。

实现方法

获取iOS的推送内容需要在delegate类中注册通知并实现回调方法。

1、在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代码:

 

    NSLog(@"测试 application didFinishLaunchingWithOptions");

    //Required

    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];

    }

    //Required

    //                [JPUSHService setupWithOption:launchOptions]

    // pushConfig.plist    appKey

    //

    //

    BOOL isProduction = NO;

    

    

    [JPUSHService setupWithOption:launchOptions appKey:APP_KEY channel:CHANNEL

                 apsForProduction:isProduction

            advertisingIdentifier:nil];

    

    




  //    ==========    接受自定义消息     ==============

    

- (void)viewDidLoad 


    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];


    [defaultCenter addObserver:self

                      selector:@selector(networkDidReceiveMessage:)

                          name:kJPFNetworkDidReceiveMessageNotification

                        object:nil];

- (void) dealloc 



 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];


    [defaultCenter removeObserver:self

                             name:kJPFNetworkDidReceiveMessageNotification

                           object:nil];



2、实现回调方法 networkDidReceiveMessage



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

    NSDictionary *userInfo = [notification userInfo];

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

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

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

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    

    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    

    NSString *currentContent = [NSString

                                stringWithFormat:

                                @"收到自定义消息:%@\ntitle:%@\ncontent:%@\nextra:%@\n",

                                [NSDateFormatter localizedStringFromDate:[NSDate date]

                                                               dateStyle:NSDateFormatterNoStyle

                                                               timeStyle:NSDateFormatterMediumStyle],

                                title, content, [self logDic:extra]];

    NSLog(@"%@", currentContent);

    

    

    

//    [_messageContents insertObject:currentContent atIndex:0];

//    

//    NSString *allContent = [NSString

//                            stringWithFormat:@"%@收到消息:\n%@\nextra:%@",

//                            [NSDateFormatter

//                             localizedStringFromDate:[NSDate date]

//                             dateStyle:NSDateFormatterNoStyle

//                             timeStyle:NSDateFormatterMediumStyle],

//                            [_messageContents componentsJoinedByString:nil],

//                            [self logDic:extra]];

//    

//    _messageContentView.text = allContent;

//    _messageCount++;

//    [self reloadMessageCountLabel];

}

- (NSString *)logDic:(NSDictionary *)dic {

    if (![dic count]) {

        return nil;

    }

    NSString *tempStr1 =

    [[dic description] stringByReplacingOccurrencesOfString:@"\\u"

                                                 withString:@"\\U"];

    NSString *tempStr2 =

    [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

    NSString *tempStr3 =

    [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];

    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];

    NSString *str =

    [NSPropertyListSerialization propertyListFromData:tempData

                                     mutabilityOption:NSPropertyListImmutable

                                               format:NULL

                                     errorDescription:NULL];

    return str;

}



参数描述:

content:获取推送的内容

extras:获取用户自定义参数

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


更多实现参考 SDK下载压缩包中的 demo。


- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    NSLog(@"推送的内容:%@",notificationSettings);

    [application registerForRemoteNotifications];

}


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

{

    

    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"];

        }

    }

    

    NSLog(@"远程推送 callid=%@",self.callid);

    

    NSLog(@"远程推送  userInfo %@ ", userInfo);

    

    [JPUSHService handleRemoteNotification:userInfo];


    

    

}


#warning 将得到的deviceToken传给SDK

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

    #warning 将获取到的token传给SDK,用于苹果推送消息使用

    

    [JPUSHService registerDeviceToken:deviceToken];


    

    [[ECDevice sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

}


#warning 注册deviceToken失败;此处失败,与SDK无关,一般是您的环境配置或者证书配置有误

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

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"apns.failToRegisterApns", Fail to register apns)

                                                    message:error.description

                                                   delegate:nil

                                          cancelButtonTitle:NSLocalizedString(@"ok", @"OK")

                                          otherButtonTitles:nil];

    [alert show];

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值