iOS推送通知操作未显示

时间:2017-07-11 20:14:30

标签: ios swift firebase push-notification apple-push-notifications

我的应用与Firebase服务器有连接,也可以发送推送通知。现在,我想更进一步,为通知添加一个动作。在抛出大量教程之后,它仍然不适合我。操作按钮未显示,您可以在此处看到:

enter image description here

这是我的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.applicationIconBadgeNumber = 0
    FirebaseApp.configure()
    registerForPushNotifications()
    return true
}

func registerForPushNotifications() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
        (granted, error) in
        print("Permission granted: \(granted)")

        guard granted else { return }

        let viewAction = UNNotificationAction(identifier: "addToCal",
                                              title: "Zum Kalender hinzufügen",
                                              options: [.foreground])

        let newsCategory = UNNotificationCategory(identifier: "NEW_SESSION",
                                                  actions: [viewAction],
                                                  intentIdentifiers: [],
                                                  options: [])
        UNUserNotificationCenter.current().setNotificationCategories([newsCategory])
        self.getNotificationSettings()
    }
}

func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

正如我在this教程中看到的那样,我还在我发送的推送通知中添加了值为“NEW_SESSION”的“category”键,但它不能正常工作。{{3} }

更新 我注意到“类别”键是通过通知传递的,所以它只是一个问题来处理它。 userInfo字典如下所示:

{ "aps" : { 
"alert" : { 
"body" : "This notification has no action", 
"title" : "Test", 
} 
}, 
"catogary" : "NEW_SESSION" 
} 

2 个答案:

答案 0 :(得分:7)

按钮不会单独出现。在支持的设备上,您必须通过3D触摸通知来显示内容或按钮。在不受支持的设备上,您可以尝试向下滑动或向左/向右滑动按钮以显示。

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("Permission granted: \(granted)")

            guard granted else { return }

            let action = UNNotificationAction(identifier: "addToCal", title: "Zum Kalender hinzufügen", options: [])
            let category = UNNotificationCategory(identifier: "NEW_SESSION", actions: [action], intentIdentifiers: [], options: [])
            UNUserNotificationCenter.current().setNotificationCategories([category])

            self.getNotificationSettings()
        }

并添加UNUserNotificationCenterDelegate方法来处理操作。

@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {

    // Receive displayed notifications for iOS 10 devices.
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        // Print message ID.
        // Print full message.
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.actionIdentifier == "addToCal" {

            //Handel action.
        }
    }
}

并且不要忘记将委托设置为:

UNUserNotificationCenter.current().delegate = self

有效载荷格式

{
    "aps" : {
              "category" : "NEW_MESSAGE_CATEGORY"
               "alert" : {
                        "body" : "Acme message received from Johnny Appleseed",
                        "title" : "Test",
                    },
               "badge" : 3,
             },
}

答案 1 :(得分:1)

对我来说,当我在“ aps”词典中包含“类别”时,它可以工作,而当我在“ aps”之外时,则不起作用。而且,你们是否注意到在Devhess发布的有效载荷中,该载荷是对他的问题的更新,“类别”字段的拼写错误。因此,这里没有“类别”,而是“类别”。这也可能是问题所在。 这个有效载荷对我有用:

for of x 88,105 ops/sec ±0.36% (89 runs sampled) forEach x 81,532 ops/sec ±0.55% (90 runs sampled) Fastest is for of