这篇文章是搬迁自我自己的新浪博客的,因为无法自动搬迁所以就自己动手了
前段时间公司的项目需要用到推送所以就这个方面做了些工作,现在想总结出来给大家共同分享。当然了,因为本人水平有限,有错漏的地方欢迎大家指正。
首先就是最繁琐最重要的注册证书了,关于证书的注册很多前辈已经总结 的很详细了,我在这里就不多加赘述。这些链接都是比较全面的关于证书注册的教程大家可以先看看http://blog.csdn.net/shenjie12345678/article/details/41120637,http://www.cocoachina.com/industry/20130321/5862.html这两篇文章都介绍了证书注册的一些知识,内容差不多。
我这篇文章将着重介绍证书生成后所要做的工作。首先你要生成服务端的证书,在这里我就介绍怎样生成java服务器的证书:
(1)将.cer文件转换成.pem文件 openssl x509 -in aps_development.cer -inform der -out PushDevelopment.pem
(2)将.p12文件转换为.pem文件 openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
(3)将现在生成的两个.pem文件生成.p12文件 openssl pkcs12 -export -in PushDevelopment.pem -inkey PushChatKey.pem -out pushCert.p12 -name “apns-cert”
通过这三步你就可以获取到java服务器所需要的证书了,关于生成php服务器的证书的前面给的连接中都有涉及。
然后你要在你的工程中注册推送,在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(nullable NSDictionary )launchOptions NS_AVAILABLE_IOS(3_0)方法中可以注册远程推送
关于注册推送的部分我写 了个方法:
(void)iOSPushActionApplication:(UIApplication *)application {
#ifdef __IPHONE_8_0
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSetting ssettingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlertcategories:nil];
[[UIApplicationsharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
#else
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
}
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
[application registerForRemoteNotifications];
}
#endif
注册推送完成后,appdelegate还有几个关于推送的代理方法,我们可以在这几个方法中对推送进行处理:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *st = [NSStringstringWithFormat:@"%@",deviceToken];
NSString *string = [st substringWithRange:NSMakeRange(1, st.length-2)];
NSString *substring = [string stringByReplacingOccurrencesOfString:@" "withString:@""]; //这样获取的字符串就是服务器所需要的deviceToken
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //下面的处理是为了点开推送后角标可以清零
[[UIApplicationsharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplicationsharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSLog(@"Regist fail%@",error);
}
此外关于推送的条数是服务器控制的(app图标上的角标数),服务端向苹果的服务器所发送的json串有一个key就是控制推送条数的,写什么数字,角标显示的就是什么数字。{“aps”:{“alert”:”这是iOS的推送测试.”,”badge”:1}},badge这key表示的就是角标数
最后,一般我们证书生成后都想抛开服务端自己测试测试,这样你可以使用一个第三方的工具PushMeBaby。
你下载了这个PushMeBaby项目后首先要导入你的aps_development.cer证书,然后你要填入你获取到的deviceToken。
在这个项目的ApplicationDelegate类的init方法中修改如下两行代码,将它改成你自己的信息:
self.deviceToken = @"8d33fbe9 c0d05bb0 325dd3e1 92a79fda c277ed85 3931c4d6 3bd15676 1b27572e"; //填写你自己的deviceToken
self.certificate = [[NSBundlemainBundle] pathForResource:@"aps_development"ofType:@"cer"]; //填写你自己的证书