iOS本地通知和远程通知的运用
发布时间:2023-04-04 11:14:56 所属栏目:教程 来源:
导读:iOS的通知即在方式上分为两种,一种是在开发应用中就固定时间或者固定条件下发送的通知即本地通知;另一种就是远程通知,由开发者向客户端提供的远程通知发送的方式。
本地通知 (Local Notification)
iOS本地通
本地通知 (Local Notification)
iOS本地通
|
iOS的通知即在方式上分为两种,一种是在开发应用中就固定时间或者固定条件下发送的通知即本地通知;另一种就是远程通知,由开发者向客户端提供的远程通知发送的方式。 本地通知 (Local Notification) iOS本地通知是在程序中指定某个时间,或者在多少时间倒计时,或者在特定条件之后,出现在设备的状态栏消息中的功能。 iOS为我们提供了几种实现方法,其中最简单的是使用 UILocalNoticification,在iOS 10.0 之后苹果推出了更加强大的 UNUserNotification; 当然了还有一种方法是使用 Noticfication Extension 通知扩展实现自定义通知。 >> 下面是具体本地通知体现在代码中的实现: > 1. 在AppDelegate.m中application方法中对通知方法执行注册 >> 1. 在Appdelegate.h中 -- 实现必要的通知注册 >> -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /* UIlocalnotification -- 本地通知的使用 */ if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { UIUserNotificationSettings *settings = [UIUserNotificationSettingssettingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [application registerUserNotificationSettings:settings]; } /* UNUserNotification -- 本地通知注册使用 */ [self initNoticWithDic:launchOptions]; return YES; } #pragma mark - 通知的初始化和 `点击通知` 的逻辑调用 --- 在程序被杀死的情况下调用否则未被杀死调用下面的方法 - (void)initNoticWithDic:(NSDictionary *)launchOptions{ // Method--1 if ([[launchOptions allKeys]containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) { // 判断发送远程通知点击进入 }else if ([[launchOptions allKeys]containsObject:UIApplicationLaunchOptionslocalnotificationKey]) { // 通过本地通知点击进入 }else{ // 正常进入程序 } // Method--2--UNUserNotificationCenter另一种注册通知方法的使用 if(@available(iOS 10.0,*)){ UNUserNotificationCenter *noticCenter = [UNUserNotificationCenter currentNotificationCenter]; [noticCenter requestAuthorizationWithOptions:(UNAuthorizationoptionBadge | UNAuthorizationoptionSound | UNAuthorizationoptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"notic-通知开启"); } else { NSLog(@"notic-关闭通知"); } }]; [noticCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"notic-settings-%@", settings); }]; } } 获取用户设置 -- 判断是否允许发送通知的回调,以便可进行相应的处理。 如果点击 允许 发送通知则 notificationSettings.type有值,否则点击 不允许 其值为0 /** * 1. 查看注册成功的通知类型 * 2. 拿到注册结果进行自定义操作(获取发送通知权限失败予以提示) */ 在 AppDelegate 中直接实现即可实现自动回调 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ // 如果点击允许发送通知则 `notificationSettings.type`有值,否则其值为0 if (notificationSettings.types && UIUserNotificationTypeBadge) { NSLog(@"Badge Nofitication type is allowed"); } if (notificationSettings.types && UIUserNotificationTypeAlert) { NSLog(@"Alert Notfication type is allowed"); } if (notificationSettings.types && UIUserNotificationTypeSound) { NSLog(@"Sound Notfication type is allowed"); } } > 2. 在AppDelegate.m中 响应通知的点击进入 和 回调操作 程序没有被杀死(处于前台或后台),点击通知后会调用此程序方法 - (void)application:(UIApplication *)application didReceivelocalnotification:(UIlocalnotification *)notification { // 必须要监听--应用程序在后台的时候进行的跳转 if (application.applicationState == UIApplicationStateInactive) { NSLog(@"进行界面的跳转"); // 如果在上面的通知方法中设置了一些,可以在这里打印额外信息的内容,就做到监听,也就可以根据额外信息,做出相应的判断 NSLog(@"%@", notification.userInfo); //点击弹窗之后会出现的视图 // UIView *redView = [[UIView alloc] init]; // redView.frame = CGRectMake(0, 0, 00, 100); // redView.backgroundColor = [UIColor redColor]; // [self.window.rootViewController.view addSubview:redView]; } } > 3.1 封装UIlocalnotification通知方法的创建 >> 2. + (void)sendLocalNotificaiton { // 1.创建本地通知 UIlocalnotification *localNotic = [[UIlocalnotification alloc] init]; // 2.设置通知触发的时间 -- 所有时间触发时间是从开始创建这个通知开始计算,并且只有此应用不在活跃状态时才会发出通知 // 2.1 倒计时设置为通知时间 localNotic.fireDate = [NSDate dateWithTimeIntervalSinceNow:3.0]; // 2.2 设置固定时间 NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]]; dateComponents.hour = 20; //20h dateComponents.minute = 0; // 00min NSDate *fireDate = [calendar dateFromComponents:dateComponents]; notification.fireDate = fireDate; // 3.设置通知的内容 -- 通知内容是在本次创建通知时不可变更的,在下次再创建通知后,会在执行创建一次通知。 localNotic.alertBody = NSLocalizedString(@"localnotificationContent_title", nil); // 4.设置滑块的文字(锁屏状态下:滑动来“解锁”) localNotic.alertAction = @"解锁"; // 5.决定alertAction是否生效 localNotic.hasAction = YES; // 6.设置点击通知的启动图片 localNotic.alertLaunchImage = @"123Abc"; // 7.设置alertTitle localNotic.alertTitle = NSLocalizedString(@"localnotificationTitle_title", nil); // 8.设置有通知时的音效 -- 音效文件可提前加入应用bundle中 localNotic.soundName = @"buyao.wav"; // 9.设置应用程序图标右上角的表示通知数目的数字 localNotic.applicationIconBadgeNumber = 1; // 10.设置通知的useInfo -- 这里设置的userInfo信息主要可以在删除此通知通过获取key和value对应此通知 localNotic.userInfo = @{@"localNoticKey" : @"localNoticValue"}; // 11.将通知加入系统通知队列中 [[UIApplication sharedApplication] schedulelocalnotification: localNotic]; } > 3.2 UNUserNotification通知方法的创建 -- 还可创建图片通知 + (void)packageUNUSerNotificationCreate:(Nsstring *)imgPathStr withBodyStr:(Nsstring *)bodyStr { NSURL *url = [NSURL fileURLWithPath:imgPathStr]; UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttachment" URL:url options:nil error:nil]; if(imageAttachment == nil){ // 如果是空就发送一次本地不带图片通知 return; } UNMutableNotificationContent *noticeContent = [[UNMutableNotificationContent alloc] init]; noticeContent.attachments = @[imageAttachment]; noticeContent.title = NSLocalizedString(@"AppNavTitle", @"应用标题名"); //content.subtitle = @""; //副标题不再调用 noticeContent.body = bodyStr; noticeContent.sound = [UNNotificationSound defaultSound]; // 如果repeats为YES,那么triggerWithTimeInterval的值要大于60s NSInteger countDownInt = 3600; // 1h-3600 UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:countDownInt repeats:NO]; // 时间设置为不重复的只是执行一次发送通知 Nsstring *requestIdentifier = @"unfinishID"; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:noticeContent trigger:trigger1]; // 发送通知 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { NSLog(@"notic-发送通知出错: %@", error); }]; (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
