APNS 学习总结(三)

来源:互联网 发布:手机动漫主题软件 编辑:程序博客网 时间:2024/05/22 21:36

Managing Your App’s Notification Support

app必须在加载的时候就配置好支持本地和远程通知。特别的,你必须进一步配置你的app如果他要做以下的这些事:

  • 显示alerts,播放声音,或者显示badges
  • 显示用于通知的自定义button

在iOS 和 tvOS不要在application:didFinishLaunchingWithOptions:之后配置通知。在watchos中,不要在applicationDidFinishLaunching方法之后配置通知。支持远程通知还需要额外的配置。

Requesting Authorization to Interact with the User

在ios,tvos和watchos中,app必须有显示alerts,播放声音或者在app icon上显示badge的权限。用户可以在设置选项中配置这些权限。

调用requestAuthorizationWithOptions:completionHandler:方法来请求权限。如果你的app拥有这些权限,系统就会调用你的completion handler block,ranted 参数置为yes。如果一个或者多个类型的权限不允许,那么参数就会设置为no。

Listing 2-1Requesting authorization for user interactions

objc

  1. UNUserNotificationCenter*center= [UNUserNotificationCentercurrentNotificationCenter];
  2. [centerrequestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionSound)
  3. completionHandler:^(BOOLgranted,NSError* _Nullableerror){
  4. // Enable or disable features based on authorization.
  5. }];

swift

  1. let center =UNUserNotificationCenter.current()
  2. center.requestAuthorization(options: [.alert, .sound]) { (granted,error)in
  3. // Enable or disable features based on authorization.
  4. }

当你的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当用户取消了通知界面且没有采取任何其他的行动时。

Listing 2-2Creating and registering a notification category

objc

  1. UNNotificationCategory*generalCategory= [UNNotificationCategory
  2. categoryWithIdentifier:@"GENERAL"
  3. actions:@[]
  4. intentIdentifiers:@[]
  5. options:UNNotificationCategoryOptionCustomDismissAction];

  6. // Register the notification categories.
  7. UNUserNotificationCenter*center= [UNUserNotificationCentercurrentNotificationCenter];
  8. [centersetNotificationCategories:[NSSetsetWithObjects:generalCategory,nil]];

swift

  1. let generalCategory =UNNotificationCategory(identifier:"GENERAL",
  2. actions: [],
  3. intentIdentifiers: [],
  4. options: .customDismissAction)

  5. // Register the category.
  6. let center =UNUserNotificationCenter.current()
  7. 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

Listing 2-3Defining custom actions for a category
  1. UNNotificationCategory*generalCategory= [UNNotificationCategory
  2. categoryWithIdentifier:@"GENERAL"
  3. actions:@[]
  4. intentIdentifiers:@[]
  5. options:UNNotificationCategoryOptionCustomDismissAction];

  6. // Create the custom actions for expired timer notifications.
  7. UNNotificationAction*snoozeAction= [UNNotificationAction
  8. actionWithIdentifier:@"SNOOZE_ACTION"
  9. title:@"Snooze"
  10. options:UNNotificationActionOptionNone];

  11. UNNotificationAction*stopAction= [UNNotificationAction
  12. actionWithIdentifier:@"STOP_ACTION"
  13. title:@"Stop"
  14. options:UNNotificationActionOptionForeground];

  15. // Create the category with the custom actions.
  16. UNNotificationCategory*expiredCategory= [UNNotificationCategory
  17. categoryWithIdentifier:@"TIMER_EXPIRED"
  18. actions:@[snoozeAction,stopAction]
  19. intentIdentifiers:@[]
  20. options:UNNotificationCategoryOptionNone];

  21. // Register the notification categories.
  22. UNUserNotificationCenter*center= [UNUserNotificationCentercurrentNotificationCenter];
  23. [centersetNotificationCategories:[NSSetsetWithObjects:generalCategory,expiredCategory,
  24. nil]];

swift

  1. let generalCategory =UNNotificationCategory(identifier:"GENERAL",
  2. actions: [],
  3. intentIdentifiers: [],
  4. options: .customDismissAction)

  5. // Create the custom actions for the TIMER_EXPIRED category.
  6. let snoozeAction =UNNotificationAction(identifier:"SNOOZE_ACTION",
  7. title: "Snooze",
  8. options: UNNotificationActionOptions(rawValue:0))
  9. let stopAction =UNNotificationAction(identifier:"STOP_ACTION",
  10. title: "Stop",
  11. options: .foreground)

  12. let expiredCategory =UNNotificationCategory(identifier:"TIMER_EXPIRED",
  13. actions: [snoozeAction,stopAction],
  14. intentIdentifiers: [],
  15. options: UNNotificationCategoryOptions(rawValue:0))

  16. // Register the notification categories.
  17. let center =UNUserNotificationCenter.current()
  18. 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:

  1. 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:方法将它们移除掉。

0 0
原创粉丝点击