一. 前言
推送是我们平时开发中常用的一种机制,无论iOS还是Android系统都有推送,推送可以让不在前台运行的app,告知用户app内部发生的事情,可以提高app的打开次数,增加日活。
iOS中的推送分为
(1)本地推送通知(Local Notification)
(2)远程推送通知(Remote Notification)
我们在平时的开发中,使用远程推送可能比较多,远程推送依赖于服务器,需要联网才能收到,本地推送无需联网,添加好定时器即可在指定时间发送推送,平时使用场景多是闹钟,提醒等。
2. 本地推送(Local Push)
3. push交互(示例基于iOS8.0及以上)
(1)注册通知,获取用户授权
#import <UserNotifications/UserNotifications.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self registerAPN]; return YES; }
- (void)registerAPN {
if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; } else { UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; } }
|
(2)添加通知
- (void)addLocalNotice { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"测试标题"; content.subtitle = @"测试通知副标题"; content.body = @"测试通知的具体内容"; content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"]; content.badge = @1; NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow]; UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];
NSString *identifier = @"noticeId"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) { NSLog(@"成功添加推送"); }]; }else { UILocalNotification *notif = [[UILocalNotification alloc] init]; notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; notif.alertBody = @"你已经10秒没出现了"; notif.userInfo = @{@"noticeId":@"00001"}; notif.applicationIconBadgeNumber = 1; notif.soundName = UILocalNotificationDefaultSoundName; notif.repeatInterval = NSCalendarUnitWeekOfYear; [[UIApplication sharedApplication] scheduleLocalNotification:notif]; } }
|
(3)移除通知
- (void)removeOneNotificationWithID:(NSString *)noticeId { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { for (UNNotificationRequest *req in requests){ NSLog(@"存在的ID:%@\n",req.identifier); } NSLog(@"移除currentID:%@",noticeId); }]; [center removePendingNotificationRequestsWithIdentifiers:@[noticeId]]; }else { NSArray *array=[[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification *localNotification in array){ NSDictionary *userInfo = localNotification.userInfo; NSString *obj = [userInfo objectForKey:@"noticeId"]; if ([obj isEqualToString:noticeId]) { [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; } } } }
- (void)removeAllNotification { if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center removeAllPendingNotificationRequests]; }else { [[UIApplication sharedApplication] cancelAllLocalNotifications]; } }
|
(4)检查授权情况
- (void)checkUserNotificationEnable { if (@available(iOS 10.0, *)) { __block BOOL isOn = NO; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { if (settings.notificationCenterSetting == UNNotificationSettingEnabled) { isOn = YES; NSLog(@"打开了通知"); }else { isOn = NO; NSLog(@"关闭了通知"); [self showAlertView]; } }]; }else { if ([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone){ NSLog(@"关闭了通知"); [self showAlertView]; }else { NSLog(@"打开了通知"); } } }
- (void)showAlertView { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"通知" message:@"未获得通知权限,请前去设置" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self goToAppSystemSetting]; }]]; [self presentViewController:alert animated:YES completion:nil]; }
- (void)goToAppSystemSetting { dispatch_async(dispatch_get_main_queue(), ^{ UIApplication *application = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([application canOpenURL:url]) { if (@available(iOS 10.0, *)) { if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) { [application openURL:url options:@{} completionHandler:nil]; } }else { [application openURL:url]; } } }); }
|
4. 参考文章:
https://www.jianshu.com/p/9b1fa25a0712
https://www.jianshu.com/p/4a23bd5e1b00