iOS消息推送原理及具体实现代码

2020-01-18 15:45:57王冬梅
app消息推送实现,app消息推送实现原理,消息推送原理,iOS,android消息推送实现

点击图中Submit,生成Development Provisioning Profiles[开发许可配置文件],这里是:iShopDevprofile.mobileprovision如下图所示:

app消息推送实现,app消息推送实现原理,消息推送原理,iOS,android消息推送实现

下载此开发许可证书(用于联机调试)。

总结,到现在为止,我们已经生成:A:*.certSigningRequest文件(在步骤(4)中使用,用于生成证书B)、B:aps_developer_identity.cer证书(在Provider[Push服务器]服务端应用使用)、C:*..mobileprovision开发许可配置文件(在Client App客户端应用联机调试使用)。

3、新建一个项目

1. 创建一个"single view application" project,为省事,你设置的"Company Identifier" + "Production“必须和step 5创建的App ID的"bundle identifier"一致。
2. 在AppDelegate.mfile的"didFinishLaunchingWithOptions" method里,添加下列代码 (用于为app register push notification feature):


//Letthedeviceknowwewanttoreceivepushnotifications
[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert)];



3.在AppDelegate.mfile里添加下列2个methods (用来handle register remote notification with device token和register error的events)
-(void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken


{
NSLog(@"Mytokenis:%@",deviceToken);
}

-(void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failedtogettoken,error:%@",error);
}

4. 运行该app in real device (simulator doesn't support push notification)。这时你会在device上看到一个popup window (该窗口只会出现一次,重装app不会再出现),提示你该app会send push notification给你,如果同意则clickOK,不同意则click "Now allow”。如果选择了OK,那么在"Setting > Notifications“里会有你的app在list里。而且这时你的Xcode output console会显示你的device token。

5: export "PushDemo" private key to a ".p12" file(该文件会在后面生成apns provider的.p12 or .pem file时用到)
1). right click "PushDemo“ private key and select "Export ..."PushDemo
2). Save the private key as “PushDemoKey.p12” file, click Save button