参考:
iOS之远程推送
苹果开发者文档Local and Remote Notification Programming Guide
iOS开发 适配Xcode8以及iOS10-推送
说明
适用于iOS10.0之前。
注册远程通知(获取deviceToken)
注册远程通知的方法
一般都是在APP启动的时候去注册远程通知,注册方法调用一般都在didFinishLaunchingWithOptions
(AppDelegate.m文件中)方法中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForRemoteNotification];
return YES;
}
- (void)registerForRemoteNotification {
//在iOS8或之后
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
}
处理注册远程通知的回调方法
// 注册成功回调方法,其中deviceToken即为APNs返回的token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[self sendProviderDeviceToken:deviceToken]; // 将此deviceToken发送给Provider
}
// 注册失败回调方法,处理失败情况
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
网友评论