1

ローカル通知をテストするために、単一のビュー コントローラーを使用してテスト アプリを作成しました。
では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()
    }
}
4

2 に答える 2

2

私の質問で申し訳ありませんが、他の誰かが同じ問題を抱えている可能性
があります。最初はタイトル/本文のみが表示されることを知りませんでした:

ここに画像の説明を入力

しかし、体の下にある薄い灰色のバーには気がつきませんでした。このバーをプルダウンすると、カスタム アクション ボタンが表示されます。

ここに画像の説明を入力

于 2016-12-19T15:48:59.140 に答える