FCM推送通知如何获取数据并显示自定义横幅

时间:2019-10-08 10:17:50

标签: ios firebase firebase-cloud-messaging

我正在开发小型应用程序,因此想向我的应用程序发送推送通知。每件事都准备好了。并且工作正常。但是,现在我想发送自定义通知,并想要显示自定义横幅,该横幅在收到fcm推送通知时显示。

我在android中很容易做到,但是在iOS中我有些困惑。这是我没有得到的东西,也不知道该怎么做

  

我想要的是:以下是我的应用要求:

  • 我要显示自定义横幅
  • 我要解析服务器发送的数据并要保存在UserDefaults中
  

我正在做什么:我正在从服务器发送推送并接收标语,但它以正文形式显示所有数据。以下是我的应用程序委托

    extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Did Receive")
    completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("Will Present")
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    print("Did Receive remote notification")
}


func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Did Receive device token")
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

}

/// Register for push notifications
func registerForPushNotification(){

    if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if error == nil{
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
    } else {

        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
        UIApplication.shared.registerForRemoteNotifications()
    }
}
  

服务器发送的结构如下,我想显示{body}标签中的一些数据

{
"to": "fBEk6eQ5I60:APA91bHR0IOZTBFJnFFxM9....GfeOGcoq-mBEUxj3ubab0x",
"priority": "high",
"force_start": 1,
"notification": {
    "body": {
        "UserId": 20054,
        "IsLogout": false,
        "DeviceId": "D58sada80-...-F8Asdasa147F",
        "Title": "Score App",
        "Message": "Today match suspended (Tue Oct-08-19 13:01)"
    },
    "title": "Score App",
    "content_available": true,
    "sound": "default",
    "priority": "high"
}
}
  

注意:我希望该应用程序在出现故障,背景甚至关闭时都显示相同的横幅

0 个答案:

没有答案