首先简单说下iOS APNS:
1.iOS
2.如果用户授予权限,iOS
3.iOS
4.当服务器应用程序需要发送推送消息时,它对
如果收件人设备在线,它接收并处理消息。如果设备离线,那么消息将会排队,然后当设备下一次在线时交付。
请求和保存设备令牌
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
在
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *tokenStr = [deviceToken description];
NSString *pushToken = [[[[tokenStr
stringByReplacingOccurrencesOfString:@"<</span>" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] retain];
// Save the token to server
NSString *urlStr = [NSString stringWithFormat:@"https://%@/push_token", RINGFULDOMAIN];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"username=%@", username]
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"&token=%@",
pushToken] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
[[NSURLConnection alloc] initWithRequest:req delegate:nil];
}
服务器应该将令牌及其相关的标识信息保存在数据库中。在大多数应用程序中,它被保存在用户配置文件数据库中。
发送一个推送消息
联系
String[] devices = {"token1", "token2};
List<</span>PushedNotification> notifications
= Push.alert("Hello World!", "keypair.p12", "password", false, devices);
JavaPNS
for (PushedNotification notification : notifications) {
if (notification.isSuccessful()) {
} else {
String invalidToken = notification.getDevice().getToken();
}
}