gpt4 book ai didi

ios - Firebase 远程通知的 UNNotificationAction 未显示

转载 作者:行者123 更新时间:2023-11-30 11:43:09 29 4
gpt4 key购买 nike

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

我的 AppDelegate 中的那些代码,

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。

如果可行,那么您应该只需要委托(delegate)方法和 requestNotificationAuthorization 方法中的代码 - 您的 notificationAction() 方法似乎不必要。

关于ios - Firebase 远程通知的 UNNotificationAction 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122864/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com