APNS 学习总结(三)
来源:互联网 发布:手机动漫主题软件 编辑:程序博客网 时间:2024/05/22 21:36
Managing Your App’s Notification Support
app必须在加载的时候就配置好支持本地和远程通知。特别的,你必须进一步配置你的app如果他要做以下的这些事:
- 显示alerts,播放声音,或者显示badges
- 显示用于通知的自定义button
在iOS 和 tvOS不要在application:didFinishLaunchingWithOptions:
之后配置通知。在watchos中,不要在applicationDidFinishLaunching
方法之后配置通知。支持远程通知还需要额外的配置。
在ios,tvos和watchos中,app必须有显示alerts,播放声音或者在app icon上显示badge的权限。用户可以在设置选项中配置这些权限。
调用requestAuthorizationWithOptions:completionHandler:
方法来请求权限。如果你的app拥有这些权限,系统就会调用你的completion handler block,ranted 参数置为yes。如果一个或者多个类型的权限不允许,那么参数就会设置为no。
UNUserNotificationCenter*center= [UNUserNotificationCentercurrentNotificationCenter];
[centerrequestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionSound)
completionHandler:^(BOOLgranted,NSError* _Nullableerror){
// Enable or disable features based on authorization.
}];
swift
let center =UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted,error)in
// Enable or disable features based on authorization.
}
当你的app第一次加载的时候,调用requestAuthorizationWithOptions:completionHandler:
方法,系统会弹出框体让用户选择是否授权。因为系统会保存用户的选择,所以一旦用户做出选择后,就不会再次弹出框体了。就是说这个框体只弹出一次。
注意: UNUserNotificationCenter
是ios 10以后的方法,10之前的是 UIUserNotificationSettings,两者的使用另外参考其他文章。
Note
用户可以通过系统设置来随意改变app的权限设置。调用getNotificationSettingsWithCompletionHandler:
方法可以知道用户目前设置了哪些权限。
Configuring Categories and Actionable Notifications
actionable通知提供给用户执行通知对应任务的快速和简单的方式。用户不是被迫来加载你的app,而是显示一个有custom action buttons的接口提供用户点击。当点击的时候,通知接口消失并且会立即执行操作。
apps必须显示的添加actionable notifications的支持。在加载时,apps必须注册一个或者多个categories。定义了app发送的通知类型。和每一个category相关联的是当一个类型的通知被投递时用户可以执行的操作。每一个category可以有多达4个actions与之相关联。
Note
通知仅仅在ios和watchos上被支持
Registering the Notification Categories for Your App
categories定义了你的app支持的通知类型和你想notification如何被呈现。
在加载阶段,使用setNotificationCategories:
方法一次性注册你的app所有的categories。在调用这个方法前,你创建一个或者多个UNNotificationCategory
类的实例并且指定category名称和options来当显示通知时使用。category名称不会被用户看到。当制定通知时,你在通知的payload中包含category名称,然后系统会使用这个来保留选项并且显示通知。
Listing 2-2
显示了如何创建一个简单的UNNotificationCategory
对象并且进行注册。这个category有一个GENERAL名字并且配置有一个自定义的dismiss action按钮。这个按钮会导致系统通知app当用户取消了通知界面且没有采取任何其他的行动时。
UNNotificationCategory*generalCategory= [UNNotificationCategory
categoryWithIdentifier:@"GENERAL"
actions:@[]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
// Register the notification categories.
UNUserNotificationCenter*center= [UNUserNotificationCentercurrentNotificationCenter];
[centersetNotificationCategories:[NSSetsetWithObjects:generalCategory,nil]];
swift
let generalCategory =UNNotificationCategory(identifier:"GENERAL",
actions: [],
intentIdentifiers: [],
options: .customDismissAction)
// Register the category.
let center =UNUserNotificationCenter.current()
center.setNotificationCategories([generalCategory])
并不是所有的通知都需要分配一个category。但是如果你不包含category,你的通知显示时就不会有任何的自定义的actions或者配置选项。
Adding Custom Actions to Your Categories
每一个注册的category都可以包含四个自定义的actions。当一个category包含自定义的actions的时候,系统会添加buttons到通知界面,每一个button都会有一个title并且对应自定义的行为。如果用户点击其中一个自定义的行为,系统就会发送对应的action标识符到你的app,需要的话会加载你的app。
Listing 2-3 extends the example inListing 2-2 by adding a new category with two custom actions.
创建一个UNNotificationAction
对象来定义自定义行为并且将它添加进你的category对象中。每一个action包含对应button的title字符串和选项。当用户选择了一个action,系统会提供你的app一个action的identifier字符串,用来告知任务执行。
objc
UNNotificationCategory*generalCategory= [UNNotificationCategory
categoryWithIdentifier:@"GENERAL"
actions:@[]
intentIdentifiers:@[]
options:UNNotificationCategoryOptionCustomDismissAction];
// 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= [UNUserNotificationCentercurrentNotificationCenter];
[centersetNotificationCategories:[NSSetsetWithObjects:generalCategory,expiredCategory,
nil]];
swift
let generalCategory =UNNotificationCategory(identifier:"GENERAL",
actions: [],
intentIdentifiers: [],
options: .customDismissAction)
// Create the custom actions for the TIMER_EXPIRED category.
let snoozeAction =UNNotificationAction(identifier:"SNOOZE_ACTION",
title: "Snooze",
options: UNNotificationActionOptions(rawValue:0))
let stopAction =UNNotificationAction(identifier:"STOP_ACTION",
title: "Stop",
options: .foreground)
let expiredCategory =UNNotificationCategory(identifier:"TIMER_EXPIRED",
actions: [snoozeAction,stopAction],
intentIdentifiers: [],
options: UNNotificationCategoryOptions(rawValue:0))
// Register the notification categories.
let center =UNUserNotificationCenter.current()
center.setNotificationCategories([generalCategory,expiredCategory])
尽管你可以为每一个category分配特定的自定义actions,系统可能只会显示其中的一两个。例如,系统只显示两个actions当在banner中显示的时候。当初始化你的UNNotificationCategory
对象,总是配置actions数组因此最相关的actions在数组的第一个。
如果使用UNTextInputNotificationAction
方法来配置action,那么系统就会允许用户输入文本作为通知响应的一部分。文本输入行为对于收集用户信息是非常有用的。例如,一个信息app可以允许用户为一个message提供自定义response。当text input action投递到你的app的时候,系统就会通过UNTextInputNotificationResponse
对象来打包用户的response。
Preparing Custom Alert Sounds
支持的音频文件格式如下:
Linear PCM
MA4 (IMA/ADPCM)
µLaw
aLaw
把音频文件放在bundle或者Library/Sounds 文件夹下。自定义的声音文件必须短于30秒,如果大于30秒的话,系统默认的声音就会播放。
You can use the afconvert
tool to convert sounds. For example, to convert the 16-bit linear PCM system soundSubmarine.aiff
to IMA4 audio in a CAF file, use the following command in the Terminal app:
afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
Managing Your App’s Notification Settings
可以使用UNNotificationSettings
对象来调整你的app通知相关的代码。
Managing Delivered Notifications
当本地和远程的通知都不直接被你的app或者用户所管理的话,它们会在notification center中显示。getDeliveredNotificationsWithCompletionHandler:
可以获得在notification center中仍然显示的通知列表。如果你发现任何过期的通知并且不应该再次呈现给用户,你就可以使用removeDeliveredNotificationsWithIdentifiers:
方法将它们移除掉。
- APNS 学习总结(三)
- APNS 学习总结(一)
- APNS 学习总结(二)
- APNS 学习总结(四)
- APNS 学习总结(五)
- (三)APNS 消息推送 发布
- APNs-百度推送使用总结(一)
- APNs-百度推送使用总结(二)
- 学习总结(三)
- APNS问题总结
- 苹果apns推送总结
- iphone推送apns学习
- SQL学习总结(三)
- jQuery学习总结(三)
- WCF学习总结(三)
- jQuery学习总结(三)
- ajax 学习总结(三)
- jQuery学习总结(三)
- Java_容器_Collection_增强for循环
- iOS - 开发中监听系统键盘删除按钮 , 回车按钮小技巧
- Eclipse构建Maven项目
- 《C++ Primer Plus(第六版)》(28)(第十四章 C++中的代码重用 笔记)
- 【9-1-6】链表的插入
- APNS 学习总结(三)
- 仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时
- MySQL里用一个表的数据更新另一个表
- jquery显示和隐藏
- Android So简单加固
- Android中的Fragment
- LaTex-merge pdfs into one file
- Linux串口编程
- 欢迎使用CSDN-markdown编辑器