一、注册苹果APNS远程推送
在didFinishLaunchingWithOptions:中调用此方法
/**
注册苹果推送,获取deviceToken用于推送
@param application application
@param launchOptions application启动参数
*/
- (void)registerAPNS:(UIApplication *)application launchOptions:(NSDictionary *)launchOptions
{
CGFloat systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// granted
NSLog(@"User authored notification.");
[application registerForRemoteNotifications];
} else {
// not granted
NSLog(@"User denied notification.");
}
}];
}else if (systemVersion >= 8.0) {
// iOS >= 8 Notifications
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
}else {
// iOS < 8 Notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
}
}
注册完成回调方法
/*
* 苹果推送注册成功回调,将苹果返回的deviceToken上传到服务器
*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
/*
* 苹果推送注册失败回调
*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
二、发送本地推送的方法:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = @"NotificationTitle";//推送显示的标题
localNotification.fireDate = [NSDate date];//设置推送时间
localNotification.timeZone = [NSTimeZone defaultTimeZone];//设置时区
localNotification.repeatInterval = NSCalendarUnitDay;//设置重复的间隔
localNotification.soundName = UILocalNotificationDefaultSoundName;//推送声音
localNotification.alertBody = @"NotificationContent";//推送内容
localNotification.applicationIconBadgeNumber = 1; //显示在icon上的红色圈中的数子
localNotification.userInfo = @{@"content":localNotification.alertBody};//userInfo中的信息
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; //立即发起本地推送
// UIUserNotificationSettings *localNotificationSettings = [UIUserNotificationSettings settingsForTypes:1 << 2 categories:nil];
// [[UIApplication sharedApplication] registerUserNotificationSettings:localNotificationSettings];
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
三、收到本地推送通知和远程推送通知及点击通知所走的方法
根据系统版本分为以下几个方法,获取到userInfo之后即可处理相关信息。
1.Version < iOS10.0
- App已经杀死,点击远程和本地推送的消息时调用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSLog(@"userInfo: %@,",[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
}
- App处于前台收到远程推送消息,或者后台(未杀死)点击远程推送消息时调用
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
NSLog(@"userInfo : %@",userInfo);
if (application.applicationState == UIApplicationStateActive) {
//app在前台
}else{
//app在后台点击远程推送
}
}
- App处于前台收到本地推送消息,或者后台(未杀死)点击本地推送消息时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(@"userInfo : %@",notification.userInfo);
if (application.applicationState == UIApplicationStateActive) {
//app在前台
}else{
//app在后台点击远程推送
}
}
2.Version >= iOS10.0
- App已经杀死,点击远程和本地推送的消息时调用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSLog(@"userInfo: %@,",[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]);
}
UNUserNotificationCenterDelegate 的代理方法
- App处于前台收到本地推送或者远程推送时调用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0){
NSLog(@"userInfo : %@",notification.request.content.userInfo);
}
- App处于后台(未杀死)点击本地推送或者远程推送时调用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0) __TVOS_PROHIBITED{
NSLog(@"userInfo : %@",notification.request.content.userInfo);
}
PS:应该就这些了吧,如有错误请留言撒~