我是靠谱客的博主 震动银耳汤,这篇文章主要介绍OC_本地消息通知,现在分享给大家,希望可以做个参考。

今天做笔试考到这个了,一点都不懂,所以先把这一块看了一看。

简介

消息通知可以在程序处于后台或者关闭状态,向使用者推送信息,例如接收到消息,或者有新的推送等。

当接收到消息通知时,一般会有以下几种表现:

  • 在屏幕顶部显示横幅
  • 在屏幕显示UIAlertView提示信息
  • 在锁屏界面显示横幅
  • APP的角标显示数字
  • 提示音

iOS中提供了2中推送通知

  • 本地消息通知
  • 远程推送通知

本地通知

本地通知的特点就是不需要联网,使用场景类似,闹钟,简单的小游戏等。

本地通知案例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //--------注册通知 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; //--------获取权限(角标,通知,音效) [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"seccess"); } }]; //--------设置通知内容 UNMutableNotificationContent *content = [UNMutableNotificationContent new]; content.title = @"这是一个本地通知"; content.subtitle = @"这是本地通知副标题"; content.body = @"这是本地通知的正文内容"; content.badge = @1; // content.sound = [UNNotificationSound soundNamed:@""]; //--------设置触发机制 UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"REQUEST" content:content trigger:trigger]; //---------添加通知到通知中心 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { NSLog(@"error : %@",error); }]; return YES; } 复制代码

以上就是个简单的案例,遵循以下几个步骤

  • 实例化对象,请求获取权限
  • 这只通知内容
  • 设置触发机制
  • 添加到通知中心

详细


首先,本地通知需要注册在 AppDelegate.mapplicationDidFinishLaunchingWithOptions 之中, 我们要使用头文件 #import <UserNotifications/UserNotifications.h>

然后进行注册,即:

复制代码
1
2
3
4
5
6
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"seccess"); } }]; 复制代码

这里是获取了 UNUserNotificationCenter 的当前 NotificationCenter 然后用它调用方法获取权限

这里的权限一共有以下几种:

复制代码
1
2
3
4
5
6
7
typedef NS_OPTIONS(NSUInteger, UNAuthorizationOptions) { UNAuthorizationOptionBadge = (1 << 0), //角标 UNAuthorizationOptionSound = (1 << 1), //提示音 UNAuthorizationOptionAlert = (1 << 2), //提示 UNAuthorizationOptionCarPlay = (1 << 3), //一个车载系统提示 } 复制代码

通过 | 符号可以混合使用。


接着需要使用 UNMutableNotificationContent 来创建需要通知的内容,他有以下可以呈现的属性:

  • badge : 角标增加个数
  • body : 通知内容
  • sound : 通知提示音 需要使用类 UNNotificationSound
  • subtitle : 通知副标题
  • title : 通知标题
  • attachments :附件,需要使用类 UNNotificationAttachment
  • categroy : 交互 提示音格式必须是 aiff/wav/caf,且提示音长度不能超过 30 s。

添加附件方法:

复制代码
1
2
3
4
NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"sport" ofType:@"png"]; UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil]; content.attachments = @[imageAttachment]; 复制代码

添加交互的方法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//点击可以显示文本输入框 UNTextInputNotificationAction *action1 = [UNTextInputNotificationAction actionWithIdentifier:@"replyAction" title:@"文字回复" options:UNNotificationActionOptionNone]; //点击进入应用 UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"enterAction" title:@"进入应用" options:UNNotificationActionOptionForeground]; //点击取消,没有任何操作 UNNotificationAction *action3 = [UNNotificationAction actionWithIdentifier:@"cancelAction" title:@"取消" options:UNNotificationActionOptionDestructive]; //通过UNNotificationCategory对象将这几个action行为添加到通知里去 UNNotificationCategory *categroy = [UNNotificationCategory categoryWithIdentifier:@"Categroy" actions:@[action1,action2,action3] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //将categroy赋值到通知内容上 content.categoryIdentifier = @"Categroy"; //设置通知代理,用于检测点击方法 [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; /* add request and notificaiton code ... */ 复制代码

同时还需要实现其代理方法进行交互。


触发机制 UNNotificationTrigger 可以设置以下几种触发类型:

  • UNTimeIntervalNotificationTrigger : 以时间间隔触发(必须长于60s)
  • UNCalendarNotificationTrigger :以指定日期触发
复制代码
1
2
3
4
5
NSDateComponents *components = [[NSDateComponents alloc] init]; components.hour = 7; components.minute = 0; // components 日期 UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; 复制代码
  • UNLocationNotificationTrigger : 位置触发器,用于到某一范围之后,触发通知

最后

以上就是震动银耳汤最近收集整理的关于OC_本地消息通知的全部内容,更多相关OC_本地消息通知内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部