本地通知不会显示自定义操作按钮

时间:2016-12-19 09:23:05

标签: ios ios10 uilocalnotification unusernotificationcenter

为了测试本地通知,我用一个视图控制器编写了一个测试应用程序 在viewDidLoad中,我设置了自定义操作,通知类别和userNotificationCenter委托 在viewDidAppear中,我设置了通知内容,设置了5秒后触发的触发器,创建了通知请求,并将其添加到通知中心。

我期待以下内容:

前景模式:
当应用程序启动时,它应该在5秒后显示前景中的通知。之前,应该调用委托函数“willPresent notification”。

背景模式:
但是,如果在触发器触发之前按下主页按钮将应用程序置于后台,则应在主屏幕中显示通知,并且不会调用代理功能“willPresent notification”。 在显示通知后,用户可以点击操作按钮 这应该将应用程序置于前台,并触发“didReceive response”委托功能。

会发生什么:
从未显示的操作按钮,仅标题和正文 当我点击主体时,使用默认操作标识符触发委托功能“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()
    }
}

2 个答案:

答案 0 :(得分:1)

对不起我的问题,但也许其他人有同样的问题:
我根本不知道,首先,只显示标题/正文:

enter image description here

然而,我并不知道身体下方的细灰色条纹。如果下拉此栏,则会显示自定义操作按钮:

enter image description here

答案 1 :(得分:1)

更新:从iOS 10测试版2开始,3D预触摸设备上也会提供丰富的通知。下拉常规通知即可查看。

确保您在iPhone6s / iPhone6s以及模拟器/设备上进行测试,它似乎不适用于3D前触摸设备。

在iPhone6模拟器上,尝试点击并向下拖动您获得的股票通知,您应该会看到自定义用户界面。

相关问题