ios 8.0 下载并安装 iOS Push的前世今生(3)

电脑杂谈  发布时间:2018-02-12 23:39:06  来源:网络整理

通知类别和通知Action

通知类别:类别定义了app是如何展示通知的。将类别和自定义的action关联起来,并且设置具体的选项(option)。让推送更加灵活

Action(UNNotificationAction)具体来讲长什么样:一般推送就一个横幅,下面没有按钮。Action其实就是横幅下面的按钮。截图如下

Actionable notifications的界面提供了用户可以点击的按钮,Actionable notifications让用户可以快速的执行相关的操作,不用强迫用户打开你的app(通过option设置是否打开app)。当用户点击的时候,会把相关的事件立即转发到app中处理。

在启动的时候。app可以注册一个或多个的通知类别,这些类别决定了app会发出哪中类型的通知。 通过设置UNNotificationCategoryOptions决定。

总的来讲这里有如下几个重要的类:

UNUserNotificationCenter :iOS10之后新增加的通知框架中。可以简单理解为这就是一个单例的Service。ios 8.0 下载并安装基本所有的通知设置都是通过去设置的。设置通知的入口 Manages the notification-related activities for your app or app extension.大致来讲有如下几个作用:

请求通知权限

声明app支持的通知类型及自定义action。

安排发出通知

管理在通知中心显示的具体通知。

获取通知相关的设置

UNNotificationCategory :设置类别的名称和各个可选项。类别的名称就是这个通知的唯一标识,系统会用来查找和显示相关通知。Defines the types of notifications your app supports and the custom actions displayed for each type.

UNNotificationAction:定义响应通知的具体任务。比如设置当点击action的时候是否打开app,处理通知是否在非锁屏状态。Defines a task to perform in response to a delivered notification.

创建及注册类别

前面提到过每个通知类别可以包含最多四个自定义action。如果类别包含了自定义的action,系统会在通知界面上添加按钮,每个都会以设置的action的title为按钮的title。如果用户点击了其中任何一个action。系统将会发送相关的action标识给app,app可以使用这个标识来标识后面执行的任务。

// Create the custom actions for expired timer notifications.
UNNotificationAction* snoozeAction = [UNNotificationAction
      actionWithIdentifier:@"SNOOZE_ACTION"
      title:@"Snooze"
      options:UNNotificationActionOptionNone];
 
UNNotificationAction* stopAction = [UNNotificationAction
      actionWithIdentifier:@"STOP_ACTION"
      title:@"Stop"
      options:UNNotificationActionOptionForeground];
 
// Create the category with the custom actions.
UNNotificationCategory* expiredCategory = [UNNotificationCategory
      categoryWithIdentifier:@"TIMER_EXPIRED"
      actions:@[snoozeAction, stopAction]
      intentIdentifiers:@[]
      options:UNNotificationCategoryOptionNone];
 
// Register the notification categories.
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObjects:generalCategory, expiredCategory,
      nil]];


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shouji/article-74387-3.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...