极光推送(二):自定义推送消息

来源:互联网 发布:淘宝卖家威胁要上门 编辑:程序博客网 时间:2024/05/21 10:57

上一篇文章介绍了极光推送里面通知的相关知识,下面我们直接进入主题,介绍一下极光推送里面自定义推送消息

(1)首先,搞清楚极光里面通知和自定义推送消息之间的区别

通知 是无论在设备在前台还是后台,都可以接收到的。

自定义推送消息 是只能在前台,并且处于当前实现自定义消息的页面,必须满足这两点才可以接收到的。

(2)自定义推送消息的代码实现

1,首先在需要接收自定义消息的控制器导入头文件

#import"JPUSHService.h"

 2,使用KVO模式进行监测

- (void)viewDidLoad {

    [superviewDidLoad];

   // Do any additional setup after loading the view, typically from a nib.

   NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];

    

    [defaultCenter addObserver:self

                      selector:@selector(networkDidReceiveMessage:)

                         name:kJPFNetworkDidReceiveMessageNotification

                        object:nil];

}


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

    NSDictionary *userInfo = [notificationuserInfo];

    //content为自定义的消息内容  extras为可选设置里面的附加字段

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

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

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

    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];

    

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

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"极光自定义消息内容提示"message:content delegate:nilcancelButtonTitle:@"确定"otherButton Titles:nil,nil];

    [alert show];

    

    NSString *currentContent = [NSString

                                stringWithFormat:

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

                                [NSDateFormatterlocalizedStringFromDate:[NSDatedate]

                                                               dateStyle:NSDateFormatterNoStyle

                                                               timeStyle:NSDateFormatterMediumStyle],

                                title, content, extra];

    NSLog(@"%@", currentContent);

}


3,最后再释放掉相关对象

- (void)dealloc

{

    NSNotificationCenter *defaultCenter = [NSNotificationCenterdefaultCenter];

    

    [defaultCenter removeObserver:self

                             name:kJPFNetworkDidReceiveMessageNotification

                           object:nil];

}


(3)最后再极光后台发送自定义推送消息,即可收到我们所需要的内容。


0 0
原创粉丝点击