网上整理的,记录一下
推送的token 改变
更新app 是只要不卸载 app 就不会改变
更新系统会改变
IOS apns Device token的获得和改变详解
远程推送的证书 会在一下情况改变
当app 更换设备或者操作系统 或者 重新安装app 的时候
iphone6 /6S 第一次运行获取不到token iPhone5s 第一次能获取到token
第一次运行获取不到 token
需要在app里面先注册通知
见最下面
申请开发推送证书,发布推送证书
下载证书到钥匙串
1、导出推送证书为P12格式 为cert.P12
导出密钥 为P12格式 为key.P12
把两个证书放到一个文件夹下
2、用控制台 CD 到文件夹下
控制台
openssl pkcs12 -clcerts -nokeys -out pushCert.pem -in cert.p12 //证书
openssl pkcs12 -clcerts -nokeys -out pushCert.pem -in cert.p12
openssl pkcs12 -nocerts -out pushKey.pem - in key.p12 //密钥
openssl pkcs12 -nocerts -out pushKey.pem -in key.p12
key.p12是上面生成的.p12文件的文件名。这时终端会让输入密码,这里的密码就是上面我们设置的密钥的密码。输入密码后回车,如果密码正确,会让我们输入新密码(一定切记),输入两次后,终端会提示成功创建PushKey.pem文件。
3、测试证书是否正常
telnet gateway.sandbox.push.apple.com 2195
4、 测试生成的pushCert.pem 和pushKey.pem 是否可用
openssl s_client -connect gateway.push.apple.com:2195 -cert pushCert.pem -key pushKey.pem
注:gateway.push.apple.com:2195用于appStore app;
gateway.sandbox.push.apple.com:2195用于沙盒app;
以上命令执行后会打印一大罗信息,最后处于可输入状态,打几个字符回车后自动断开连接即为正常
4、合并两个证书为 ck.pem
cat pushCert.pem pushKey.pem > ck.pem
5、开始测试
把下面的PHP文件和ck文件 放在同一目录下面
执行
php php.php
6、成功,成功的话手机会收到一条消息
//ssl://gateway.sandbox.push.apple.com:2195 测试的时候的地址
//ssl://gateway.push.apple.com:2195 发布以后的地址
注册通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。
if(version>=8.0f){
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}else{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
}
收到的消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ // 处理推送消息
NSLog(@"userinfo:%@",userInfo);
NSLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
}
错误信息
-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error {
NSLog(@"Registfail%@",error);
}
token
-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSLog(@"%@",deviceToken);//这里的Token就是我们设备要告诉服务端的Token码
}
注册通知
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(nonnull UIUserNotificationSettings *)notificationSettings
{
//注册通知
[application registerForRemoteNotifications];
{
//注册通知
[application registerForRemoteNotifications];
}