ios10 不显示通知自定义操作按钮

Posted

技术标签:

【中文标题】ios10 不显示通知自定义操作按钮【英文标题】:ios10 doesn't show notification custom action button 【发布时间】:2017-03-11 07:14:44 【问题描述】:

我想在我的应用收到通知时显示通知自定义操作按钮。但是什么也没发生!任何人都可以帮助我吗?谢谢。

这是我的AppDelegate.swift 代码:

class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

        // Register ourselves as a delegate so we can be notified when actions pressed.
        UNUserNotificationCenter.current().delegate = self

        // Request permission.
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) 
            granted, error in
            if granted 
                print("Approval granted to send notifications")
            
        

        self.addCategory()

        return true
    

    func addCategory() 
        // Add action.
        let stopAction = UNNotificationAction(identifier: "stop", title: "Stop", options: [])
        let snoozeAction = UNNotificationAction(identifier: "snooze", title: "Snooze", options: [])

        // Create category.
        let category = UNNotificationCategory(identifier: "reminder", actions: [stopAction, snoozeAction], intentIdentifiers: [], options: [])

        UNUserNotificationCenter.current().setNotificationCategories([category])
    

UNUserNotificationCenterDelegate

extension AppDelegate: UNUserNotificationCenterDelegate 

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 
        completionHandler()
    

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
        print("Called when a notification is delivered to a foreground app.")
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationPresentAtForeground"), object: nil)
        completionHandler([.alert])
    

【问题讨论】:

【参考方案1】:

我找到了答案。我没有设置我的通知内容类别标识符。 以下对我有用:

content.categoryIdentifier = "reminder"

【讨论】:

以上是关于ios10 不显示通知自定义操作按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何显示自定义警报以在 ios 中启用通知

无法对推送通知自定义按钮执行操作

远程通知中的自定义声音 iOS 10,swift 3

推送通知显示自定义警报

ios8自定义交互式推送通知

带有自定义声音的推送通知未在 iOS 中显示