因为业务需求考虑使用APNs 实现远程推送
(1) 程序内注册通知将token 发送给服务器(重点在token的处理)
(2)导出p12 文件提供给服务器
// 测试证书
// 生产证书
服务器端使用c#实现:代码参考 https://github.com/Redth/PushSharp/
注册远程通知
-(void)registForRemoteNotification{
UIUserNotificationType types = (UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert);
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
实现代理方法
// 注册成功
//(刚开始对对token 的处理还有点小纠结 因为注册成功后返回的类型为NSData类型的数据 而服务器端需要的是字符串 ,不知道该对Token 做什么处理才会被苹果远程推送服务器识别 以下处理方法亲测有效)
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token =
[[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"
withString:@""]
stringByReplacingOccurrencesOfString:@">"
withString:@""]
stringByReplacingOccurrencesOfString:@" "
withString:@""];
// 将token 发送给服务器即可
}
// 注册失败回调方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}