我试图为支持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()
}
}
答案 0 :(得分:0)
要通过Firebase进行iOS远程通知,需要执行多个步骤,其中许多步骤与代码无关。首先,请确保您能够发送/接收简单的纯文本通知,而无需执行操作。
一旦生效,然后查看您是否收到内置动作UNNotificationDefaultActionIdentifier和UNNotificationDismissActionIdentifier。
如果可行,那么您只需要委托方法和requestNotificationAuthorization
方法中的代码-您的notificationAction()
方法似乎是不必要的。