【问题标题】:Add buttons to dynamic notification interface动态通知界面添加按钮
【发布时间】:2017-08-24 12:14:49
【问题描述】:

我正在创建一个自定义动态通知来向用户显示。一旦我在didReceiveNotification 收到此通知@ 987654323@ 功能,我使用正确的数据设置接口出口。我的问题是我没有意识到如何在默认关​​闭按钮上方添加自定义按钮,因为通知情节提要不允许插入按钮,Apple Documentation

不要包含按钮、开关或其他交互式控件。

但我看到很多手表应用程序都有自定义操作,例如消息和 Facebook Messenger。有什么方法可以在 watchOS 的动态界面中添加自定义操作?

【问题讨论】:

    标签: apple-push-notifications watchkit


    【解决方案1】:

    您根本无法将按钮添加到动态通知界面。如果您尝试这样做,您将收到错误消息

    非法配置:通知界面不支持按钮。

    但是,除了Dismiss 按钮之外,您还可以在通知中添加系统按钮。在设置通知中心的类别时,您可以指定自定义UNNotificationActions添加到您的通知类别中。

    var categories = Set<UNNotificationCategory>()
    let myCategory = UNNotificationCategory(identifier: "MyCategory", actions: [/*your custom actions go here*/], intentIdentifiers: [], options: []) //set up the actions here
    categories.insert(myCategory)
    center.setNotificationCategories(categories)
    

    然后您可以在UNUserNotificationCenterDelegate 方法userNotificationCenter(_:didReceive:withCompletionHandler:) 中处理用户与这些操作的交互(在动态通知界面上显示为普通按钮),如下所示:

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        switch response.actionIdentifier {
        case "Ok":
            print("Ok action tapped")
        case "Dismiss":
            print("Dismiss action tapped")
        default:
            break
        }
        completionHandler()
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2016-05-24
      相关资源
      最近更新 更多