自定义推送消息

来源:互联网 发布:修改ssh 默认端口 编辑:程序博客网 时间:2024/06/05 10:11
极光推送(二):自定义推送消息 
 分类:

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

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

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

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

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

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

#import"JPUSHService.h"

 2,使用KVO模式进行监测

- (void)viewDidLoad {

    [super viewDidLoad];

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

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    

     [defaultCenter addObserver:self

                      selector:@selector(networkDidReceiveMessage:)

                          name:kJPFNetworkDidReceiveMessageNotification

                        object:nil];

}


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

    NSDictionary *userInfo = [notification userInfo];

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

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

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

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

    NSDateFormatter *dateFormatter = [[NSDateFormatter allocinit];

    

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

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"极光自定义消息内容提示" message:content delegate:nilcancelButtonTitle:@"确定" otherButtonTitles:nilnil];

    [alert show];

    

    NSString *currentContent = [NSString

                                stringWithFormat:

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

                                [NSDateFormatter localizedStringFromDate:[NSDate date]

                                                               dateStyle:NSDateFormatterNoStyle

                                                               timeStyle:NSDateFormatterMediumStyle],

                                title, content, extra];

    NSLog(@"%@", currentContent);

}


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

- (void)dealloc

{

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    

    [defaultCenter removeObserver:self

                             name:kJPFNetworkDidReceiveMessageNotification

                           object:nil];

}


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

原创粉丝点击