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

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

(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 alloc] init];

    

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

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

    [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)最后再极光后台发送自定义推送消息,即可收到我们所需要的内容。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值