点击下载的两张证书(激活操作),然后在钥匙串中,分别选中并导出.c12文件,保存在本地,就可以用这两张证书( Apple Development IOS Push Services是调试证书,Apple Push Services : 是发布证书)。
以上证书配置完成后,就可以在appdelegate.m中配置对应的推送内容了:
- (void)registerAPNS
{
//APNS 注册
#ifdef __IPHONE_8_0
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
}
#pragma mark APPLE APNS
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
if (notificationSettings.types!=UIUserNotificationTypeNone) {
setIsLocalNotification(@YES);
}else {
setIsLocalNotification(@NO);
}
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
{
//handle the actions
if ([identifier isEqualToString:@"declineAction"]){
}
else if ([identifier isEqualToString:@"answerAction"]){
}
}
#endif
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 接受apple 推送消息 可以自定义显示
// NSString *message = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
// message:message
// delegate:self
// cancelButtonTitle:@"取消"
// otherButtonTitles:@"确定", nil];
// [alertView show];
}
<pre name="code" class="objc">- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
{//获取设备号,并要后台提供一个接口,用于将用户token和设备号进行绑定
[[APIClient shareInstance] registerPhonePushMessageBy:getUserToken</span>
AppleToken:deviceToken
Success:^(NSString *successMsg)
{
}
Failure:^(NSString *faileMessage)
{
}];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error
{
}