The preface

As we all know, notifications play an integral role in iOS. Both local and remote notifications affect our user experience all the time. As a result, in iOS10, apple re-architecting push, separating UserNotifications and UserNotificationsUI into two separate frameworks. What did Apple surprise us about WWDC18?

New features

  • Grouped notifications
  • Notification Content Extensions Interactive and dynamically changing actions in push content extensions
  • Notification Management Manages push messages
  • Provisional Authorization
  • Critical Alerts Warning push

Push the grouping

With the increasing number of applications on mobile phones, especially QQ and wechat, the two major chat tools, when the mobile phone locks the screen, it is accompanied by a full screen push message. This phenomenon I do not know whether we feel inefficient and poor experience? Apple groups messages in the case of lock screen, so as to effectively improve user interaction experience. The grouping form is as follows:

Grouping form:

  • Apple automatically helps us group messages by APP;
  • If we set itthreadIdentifierAttributes are grouped based on that attribute.

let content = UNMutableNotificationContent() content.title = "Notifications Team" content.body = "WWDC session after Party "content.threadIdentifier = "notifications-team-chat"// Use this attribute to set groups. If this attribute is not set, the group is based on APPCopy the code

Summary format customization

When Apple automatically aggregates push messages, there is a summary at the bottom. The default format is n more notifications from XXX. However, we can customize this format.

  • The first kind of
Let summaryFormat = "%u "return UNNotificationCategory(Identifier: "category-identifier", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: [])Copy the code

  • Let summaryFormat = “%u From OceanFish”
let content = UNMutableNotificationContent()
content.body = "..."
content.summaryArgument = "OceanFish"
Copy the code

Different formats of the same category, Apple will merge them together; And apple will display different summaryarguments together by default

Can also through the let summaryFormat = nsstrings. LocalizedUserNotificationString (forKey: “NOTIFICATION_SUMMARY”, the arguments: Nil) for localized service

Digital custom

Sometimes another scenario occurs: two push messages are sent, one saying “you have 3 invitations” and the other saying “you have 5 invitations”. That feed will say you have 2 more messages. This is clearly not what we want! The best we can hope for is “You have eight invitations”. So how do you show this effect?

Apple provides us with another property that we can achieve with the Summary format customization above.

let content = UNMutableNotificationContent()
content.body = "..."
content.threadIdentifier = "..."
content.summaryArgument = "Song by Song"
content.summaryArgumentCount = 3
Copy the code

When multiple messages come together, Apple adds together the summaryArgumentCount values and displays them

Interactive and dynamically changing actions in the push content extension

Previously, messages did not support interactive and dynamic Action changes. For example, the interface has a hollow “like” button, which becomes a solid “like” button when the user clicks it. There’s an Acction that says “like,” and when the user clicks it turns into “dislike.”

The push interface is interactive

  • First configure the Notification Content ExtentionUUNNotificationExtensionUserInteractionEnabledforYES

  • And then the code implements
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {

    @IBOutlet var likeButton: UIButton?

    likeButton?.addTarget(self, action: #selector(likeButtonTapped), for: .touchUpInside)

    @objc func likeButtonTapped() {
        likeButton?.setTitle("♥", for: .normal)
        likedPhoto()
    }
}

Copy the code

The Action dynamic

// Notification Content Extensions class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler completion: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "like-action" { // Update state... let unlikeAction = UNNotificationAction(identifier: "unlike-action", title: "Unlike", options: []) let currentActions = extensionContext? .notificationActions let commentAction = currentActions! [1] let newActions = [ unlikeAction, commentAction ] extensionContext? .notificationActions = newActions } } }Copy the code

Click performNotificationDefaultAction () is used to push the time to start the application; DismissNotificationContentExtension () is used to close the lock screen push specific a message of the page

Manage push messages

This is mainly because Apple has added a “manage” button for messages that can be swiped left to appear.

  • Will probably Deliver the sound.
  • Turn off turns off the push
  • We can order Setttings ourselves
import UIKit import UserNotifications class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, openSettingsFor notification: UNNotification? ) {}}Copy the code

A temporary authorization

Temporary authorization is mainly reflected in the push message will have two buttons, will actively let the user to choose

let notificationCenter = UNUserNotificationCenter.current()

noficationCenter.requestAuthorization(options: [.badge,.alert,.sound,.provisional]) { (tag, error) in

}
Copy the code

Provisional can be added to the application for permission.

A warning message

Such as family safety, health, public safety, etc. This message requires the user to take action. The simplest scenario is that there is a camera installed at home, and we go to work. If there are people at home, the camera will push messages to us.

  • Certificate for https://developer.apple.com/contact/request/notifications-critical-alerts-entitlement/

  • Local Permission Application

let notificationCenter = UNUserNotificationCenter.current()
noficationCenter.requestAuthorization(options: [.badge,.alert,.sound,.criticalAlert]) { (tag, error) in

}
Copy the code

When applying for permissions, add criticalAlert.

  • The play
let content = UNMutableNotificationContent() content.title = "WARNING: LOW BLOOD SUGAR "content. body =" Glucose level at 57. "content. categoryIdentifier =" LOW Glucose - alert "content. sound = UNNotificationSound. CriticalSoundNamed (@ "warning - sound" withAudioVolume: 1.00)Copy the code
// Critical alert push payload

{
  // Critical alert push payload

  {
      "aps" : {
          "sound" : {
              "critical"1.}}"name": "warning-sound.aiff"."volume": 1.0}}Copy the code

conclusion

So far, all the pushes in WWDC have been sorted out. We do not understand the welcome message exchange

reference

  • Using, Managing, and Customizing Notifications
  • What’s New in User Notifications
  • Using Grouped Notifications

My blog

FlyOceanFish