ローカル通知をテストするために、単一のビュー コントローラーを使用してテスト アプリを作成しました。
ではviewDidLoad
、カスタム アクション、通知カテゴリ、および userNotificationCenter デリゲートを設定しました。
ではviewDidAppear
、通知内容を設定し、5 秒後に起動するトリガーを設定し、通知リクエストを作成して、通知センターに追加します。
私は次のことを期待しています:
フォアグラウンド モード:
アプリが起動されると、5 秒後にフォアグラウンドで通知が表示されます。その前に、デリゲート関数「willPresent 通知」を呼び出す必要があります。
バックグラウンド モード:
ただし、トリガーが起動する前にホーム ボタンを押してアプリをバックグラウンドにすると、ホーム画面に通知が表示され、デリゲート関数「willPresent 通知」は呼び出されません。
通知が表示された後、ユーザーはアクション ボタンをタップできます。
これにより、アプリがフォアグラウンドになり、「didReceive 応答」デリゲート関数がトリガーされます。
何が起こるか:
アクション ボタンは決して表示されず、タイトルと本文のみです。
本文をタップすると、デフォルトのアクション識別子を使用してデリゲート関数「didReceive response」がトリガーされます。
問題:
カスタム アクション ボタンが表示されないのはなぜですか?
これが私のコードです:
import UIKit
import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
let userNotificationCenter = UNUserNotificationCenter.current()
let categotyId = "categoryID"
let actionID = "actionID"
override func viewDidLoad() {
super.viewDidLoad()
userNotificationCenter.requestAuthorization(options: [.alert]) { (granted, error) in
if granted {
let okAction = UNNotificationAction(identifier: self.actionID,
title: "OK",
options: [])
let category = UNNotificationCategory(identifier: self.categotyId,
actions: [okAction],
intentIdentifiers: [],
options: [.customDismissAction])
self.userNotificationCenter.setNotificationCategories([category])
self.userNotificationCenter.delegate = self
} else {
print("local notifications not granted")
}
}
userNotificationCenter.removeAllPendingNotificationRequests()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Body", arguments: nil)
content.categoryIdentifier = categotyId
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (5), repeats: false)
let request = UNNotificationRequest.init(identifier: "requestID",
content: content,
trigger: trigger)
userNotificationCenter.add(request, withCompletionHandler: { (error) in
if let error = error {
print("Could not add notification request. Error: \(error)")
}
})
}
// MARK: - Notification Delegate
// Will be called while app is in the foreground
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Show alert to the user
print("App in foreground. Show alert.")
completionHandler([.alert])
}
// Should be called after the user tapped the action button
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let request = response.notification.request
let requestID = request.identifier
switch response.actionIdentifier {
case actionID:
print("Custom OK action triggered in background")
case UNNotificationDefaultActionIdentifier:
print("Default action triggered in background")
default:
print("Unknown action triggered in background, action identifier: \(response.actionIdentifier)")
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [requestID])
completionHandler()
}
}