未显示带有 Firebase 远程通知的 UNNotificationAction



我因尝试为支持 iOS 9、10 和 11 的远程通知添加 UNNotificationAction 按钮而被困了几天。我遵循了多个教程中的所有步骤,其中大多数都使用本地通知来显示到目前为止有效的 UNNotificationAction。但是它不适用于我的远程通知。我能够通过 Firebase 发送远程通知。如果我的代码的某些部分不正确或缺少部分,请告诉我以配置 UNNotificationAction 按钮。

我的应用程序代表中的那些代码,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    requestNotificationAuthorization(application: application)
    }
    return true
}
func requestNotificationAuthorization(application: UIApplication) {
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {granted, error in
            if (granted)
            {
                let viewAction = UNNotificationAction(identifier: "viewAction", title: "View", options: [])
                let closeAction = UNNotificationAction(identifier: "closeAction", title: "Close", options: [])
                // 2
                let buttonCategory = UNNotificationCategory(identifier: "buttonCategory", actions: [viewAction, closeAction], intentIdentifiers: [], options: [])
                // 3
                UNUserNotificationCenter.current().setNotificationCategories([buttonCategory])
                DispatchQueue.main.async {
                   application.registerForRemoteNotifications()
                }
                print("Granted")
            }
        })
    } else {
        let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: notificationAction() as? Set<UIUserNotificationCategory>)
        application.registerUserNotificationSettings(settings)
        DispatchQueue.main.async {
            application.registerForRemoteNotifications()
        }
    }
}
func notificationAction() -> NSSet {
    let firstAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    firstAction.identifier = "First_Action"
    firstAction.title = "View"
    firstAction.activationMode = .foreground
    firstAction.isDestructive = false
    firstAction.isAuthenticationRequired = false
    let secondAction: UIMutableUserNotificationAction = UIMutableUserNotificationAction()
    secondAction.identifier = "Second_Action"
    secondAction.title = "Close"
    secondAction.activationMode = .background
    secondAction.isDestructive = false
    secondAction.isAuthenticationRequired = false
    //category
    let firstCategory : UIMutableUserNotificationCategory = UIMutableUserNotificationCategory()
    firstCategory.identifier = "buttonCategory"
    let defaultAction = [firstAction,secondAction]
    let mininalAction = [firstAction,secondAction]
    firstCategory.setActions(defaultAction, for: UIUserNotificationActionContext.default)
    firstCategory.setActions(mininalAction, for: UIUserNotificationActionContext.minimal)
    //NSSet of category
    let categories = NSSet(object: firstCategory)
    return categories
}
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// iOS10+, called when presenting notification in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert])
}
// iOS10+, called when received response (default open, dismiss or custom action) for a notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    if(response.actionIdentifier == "viewAction"){
        print("viewing Action")
    }
    if(response.actionIdentifier == "closeAction"){
        print("closing Action")
    }
    completionHandler()
  }
}

通过Firebase使iOS远程通知工作有几个步骤,其中许多步骤与代码无关。首先确保您能够发送/接收简单的纯文本通知,而无需操作。

一旦它起作用,然后查看您是否收到内置操作,UNNotificationDefaultActionIdentifier和UNNotificationDismissActionIdentifier。

如果这有效,那么您应该只需要委托方法和requestNotificationAuthorization方法中的代码 - 您的notificationAction()方法似乎没有必要。

相关内容

  • 没有找到相关文章

最新更新