深入 iOS push notification

socket

APP注册Push Notification

程序内注册Push Notification

获取后的DeviceToken要作下去空格处理

1
2
3
4
5
// Get a hex string from the device token with no spaces or < >
self.deviceToken = [[[[token description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
1
2
3
4
5
NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
[set formUnionWithCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
[set formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self.deviceToken = [[token description] stringByTrimmingCharactersInSet:set];

消息格式:

1
2
3
4
5
6
7
{
"aps": {
"alert": "Hello world",
"sound": "default",
"badge": 1
}
}

接收到消息后的处理:

在UIApplicationDelegate里有个方法didReceiveRemoteNotification,即在收到推送通知时触发的方法,在这个方法里载入你的ViewController,有时需要根据推送消息的内容来载入不同页面,则需要在消息里set相应值,就是Dic结构,然后在didReceiveRemoteNotification中取得相应值来处理。

测试环境

PushSharp

https://github.com/Redth/PushSharp

push

A server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), Android (C2DM and GCM - Google Cloud Message), Windows Phone, Windows 8, Amazon, Blackberry, and (soon) FirefoxOS devices!

PushMeBaby

https://github.com/stefanhafeneger/PushMeBaby

Push Service::

Urban Airship

http://urbanairship.com/

Amazon SNS (Beta)

http://aws.amazon.com/sns/

JPush

https://www.jpush.cn/

百度云推送

http://developer.baidu.com/wiki/index.php?title=docs/cplat/push

FAQ::

AdHoc 测试

是可以编译测试的。

REF::

Apple: Local and Push Notifications for Developers
Apple: Local and Push Notification Programming Guide
Apple Push Notification Services 消息推送服务教程 EN - Part 1 EN - Part 2 中文 - 第一部分 中文 - 第二部分 中文 - 第三部分
didReceiveRemoteNotification when in background
Setting up Apple Push Notifications