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

支持的版本

r1.2.5 以后。

功能说明

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

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

实现方法

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

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

 

    NSLog(@"测试application didFinishLaunchingWithOptions");

   //Required

   if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {

       //       categories

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge|

                                                         UIUserNotificationTypeSound|

                                                         UIUserNotificationTypeAlert)

                                             categories:nil];

    }else{

       //categories    nil

        [JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|

                                                         UIRemoteNotificationTypeSound|

                                                         UIRemoteNotificationTypeAlert)

                                             categories:nil];

    }

   //Required

   //                [JPUSHService setupWithOption:launchOptions]

   // pushConfig.plist    appKey

   //

   //

   BOOLisProduction =NO;

    

    

    [JPUSHServicesetupWithOption:launchOptionsappKey:APP_KEYchannel:CHANNEL

                apsForProduction:isProduction

           advertisingIdentifier:nil];

    

    




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

    

- (void)viewDidLoad 


   NSNotificationCenter*defaultCenter = [NSNotificationCenterdefaultCenter];


    [defaultCenteraddObserver:self

                     selector:@selector(networkDidReceiveMessage:)

                         name:kJPFNetworkDidReceiveMessageNotification

                       object:nil];

- (void) dealloc 



 NSNotificationCenter*defaultCenter = [NSNotificationCenterdefaultCenter];


    [defaultCenterremoveObserver:self

                            name:kJPFNetworkDidReceiveMessageNotification

                          object:nil];



2、实现回调方法 networkDidReceiveMessage



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

   NSDictionary*userInfo = [notificationuserInfo];

   NSString*title = [userInfovalueForKey:@"title"];

   NSString*content = [userInfovalueForKey:@"content"];

   NSDictionary*extra = [userInfovalueForKey:@"extras"];

   NSDateFormatter*dateFormatter = [[NSDateFormatteralloc] init];

    

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

    

   NSString*currentContent = [NSString

                               stringWithFormat:

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

                                [NSDateFormatterlocalizedStringFromDate:[NSDatedate]

                                                               dateStyle:NSDateFormatterNoStyle

                                                               timeStyle:NSDateFormatterMediumStyle],

                                title, content, [selflogDic: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(![diccount]) {

       returnnil;

    }

   NSString*tempStr1 =

    [[dicdescription]stringByReplacingOccurrencesOfString:@"\\u"

                                                withString:@"\\U"];

   NSString*tempStr2 =

    [tempStr1stringByReplacingOccurrencesOfString:@"\""withString:@"\\\""];

   NSString*tempStr3 =

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

   NSData*tempData = [tempStr3dataUsingEncoding:NSUTF8StringEncoding];

   NSString*str =

    [NSPropertyListSerializationpropertyListFromData:tempData

                                    mutabilityOption:NSPropertyListImmutable

                                              format:NULL

                                    errorDescription:NULL];

   returnstr;

}



参数描述:

content:获取推送的内容

extras:获取用户自定义参数

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


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


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

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

    [applicationregisterForRemoteNotifications];

}


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

{

    

   self.callid=nil;

   NSString*userdata = [userInfoobjectForKey:@"c"];

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

   if(userdata) {

       NSDictionary*callidobj = [NSJSONSerializationJSONObjectWithData:[userdatadataUsingEncoding:NSUTF8StringEncoding]options:NSJSONReadingMutableLeaveserror:nil];

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

       if([callidobjisKindOfClass:[NSDictionaryclass]]) {

           self.callid= [callidobjobjectForKey:@"callid"];

        }

    }

    

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

    

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

    

    [JPUSHServicehandleRemoteNotification:userInfo];


    

    

}


#warning将得到的deviceToken传给SDK

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

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

    

    [JPUSHServiceregisterDeviceToken:deviceToken];


    

    [[ECDevicesharedInstance]application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

}


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

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

   UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:NSLocalizedString(@"apns.failToRegisterApns", Fail toregisterapns)

                                                   message:error.description

                                                  delegate:nil

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

                                         otherButtonTitles:nil];

    [alertshow];

}




版权声明:本文为qq_27247497原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。