APNS

APNS:

就是APP的服务端在用户未打开APP进程时,还能发通知给用户APP的服务技术

相关概念

Provider:提供推送的第三方服务系统

Device:苹果设备,例如iphone和ipad等

APNS:苹果推送消息服务,属于苹果的服务

APP:安装在苹果设备上的应用程序

DeviceToken:设备的标识,用于确定接收通知的设备及APP

Payload:推送消息的传输形式

推送使用:

1.需要去极光官网申请一个appkey,类似:478c42dw2e58e01c4a9b449a

2.配置极光推送,可以设置声音,震动,alert,不设置后面是不可以用这些功能的,启动SDK 

代码:

[JPUSHService setupWithOption:launchOptions

appKey:appKey

channel:channel

apsForProduction:isProduction];

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

}

//监听

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

[defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kJPFNetworkDidSetupNotification object:nil];

[defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kJPFNetworkDidCloseNotification object:nil];

[defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kJPFNetworkDidRegisterNotification object:nil];

[defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil];

[defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

[defaultCenter addObserver:self selector:@selector(serviceError:) name:kJPFServiceErrorNotification object:nil];

2.注册设备,每次启动程序都要注册,可以通过前面的监听,判断是否建立连接,是否注册成功

[[QYPushHelper shared] setTags:

[NSSet setWithObjects:

[NSString stringWithFormat:@"test_company_%@",account.companyId],

[NSString stringWithFormat:@"test_company_ios_%@",account.companyId],

[NSString stringWithFormat:@"test_ios_user_%@",account.userId],

[NSString stringWithFormat:@"test_user_ios_%@",account.userId],

[NSString stringWithFormat:@"test_%@",account.phone],

[NSString stringWithFormat:@"test_%@",account.userId],

[NSString stringWithFormat:@"test_user_phone_%@",account.phone],

[NSString stringWithFormat:@"test_notice_user_%@",account.userId],

[NSString stringWithFormat:@"test_ios_user_%@",account.userId],nil]

alias:[NSString stringWithFormat:@"i%@",account.userId] success:^

{

}failure:^(int code, NSString *log)

{

}];

3.接收消息 

/**

*  APNS消息 程序处于后台

*

*  @param notification

*/

- (void)handleRemoteNotification:(NSDictionary *)remoteInfo

{

// Required

[JPUSHService handleRemoteNotification:remoteInfo];

QYPushResult *pushResult = [[QYPushResult alloc] init];

pushResult.pushType = QYPushTypeAPNS;

pushResult.msgid = [remoteInfo[@"_j_msgid"] longLongValue];

pushResult.content = remoteInfo[@"aps"][@"alert"];

NSMutableDictionary *extras = [[NSMutableDictionary alloc] init];

[remoteInfo enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)

{

if (![key isEqualToString:@"aps"]&&![key isEqualToString:@"_j_msgid"])

{

[extras setObject:obj forKey:key];

}

}];

pushResult.extras = extras;

pushResult.badge = [remoteInfo[@"aps"][@"badge"] integerValue];

pushResult.sound = remoteInfo[@"aps"][@"sound"];

//自己处理

[self didReceiveRemoteNotification:pushResult];

}

/**

*  非APNS消息 ,程序处于前台

*

*  @param notification

*/

- (void)networkDidReceiveMessage:(NSNotification *)notification

{

NSDictionary * userInfo = [notification userInfo];

QYPushResult *pushResult = [[QYPushResult alloc] init];

pushResult.pushType = QYPushTypeMessage;

pushResult.content = userInfo[@"content"];

pushResult.extras = userInfo[@"extras"];

[self didReceiveRemoteNotification:pushResult];

}

4.退出登录 清空tag'

//推送

[[QYPushHelper shared] setTags:[NSSet set] alias:@"" success:^{

} failure:^(int code, NSString *log) {

}];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,809评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,189评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,290评论 0 359
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,399评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,425评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,116评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,710评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,629评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,155评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,261评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,399评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,068评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,758评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,252评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,381评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,747评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,402评论 2 358

推荐阅读更多精彩内容