Почему я не могу прочитать данные push-сообщений в Swift5? - PullRequest
0 голосов
/ 26 сентября 2019

У меня проблема с тем, что я не могу сейчас прочитать данные push-сообщений.Обычно я получаю сообщение.

Я могу получать оба сообщения, когда приложение находится на переднем плане или в фоновом режиме, когда нажимаю кнопку «Домой».

Но я не вижу данные сообщения в журнале.

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate  {
...
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
       guard
           let aps = data[AnyHashable("notification")] as? NSDictionary,
           let alert = aps["alert"] as? NSDictionary,
           let body = alert["body"] as? String,
           let title = alert["title"] as? String
           else {
               // handle any error here
               return
           }

       Log.Info("Title: \(title) \nBody:\(body)")
       Messaging.messaging().appDidReceiveMessage(data)

      // Print full message.
      Log.Info(data)
    }
...



    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {

          Log.Info("fcmToken \(fcmToken)")
       }

    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
           Log.Info("remort \(remoteMessage.appData)")
    }

Данные, которые я отправляю

{

     notification : {

                            "title" : "test title.",  

                            "body" : "test context."            

                      },

     data : {

                    "image" : "http://11.111.111.111:100000000/_img/sample_01.jpg",  

                    "page_url" : "http://11.111.111.111:100000000/Point?address=",   

                    "type" : "point"       

             }

}

Журналы нельзя просмотреть ни в одной функции.Что мне не хватает?Пожалуйста, дайте мне знать, что не так.


РЕДАКТИРОВАТЬ


Я изменил неправильные данные и изменил местоположение журнала.Но у меня нет никаких логов на меня.

AppDelegate.swift

    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable: Any]) {
      // If you are receiving a notification message while your app is in the background,
      // this callback will not be fired till the user taps on the notification launching the application.
      // TODO: Handle data of notification

      // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Print full message.
        Log.Info(data)
       guard
           let aps = data[AnyHashable("notification")] as? NSDictionary,
           let body = aps["body"] as? String,
           let title = aps["title"] as? String
           else {
               // handle any error here
               return
           }

       Log.Info("Title: \(title) \nBody:\(body)")
       Messaging.messaging().appDidReceiveMessage(data)
    }

отправить тестовое сообщение в базе данных

enter image description here

У меня есть две функции messaging в дополнение к application функциям приложения.Эти функции сообщений мне не нужны?Эти функции никогда не показывали мне журнал.

1 Ответ

0 голосов
/ 26 сентября 2019

Это решило эту проблему с помощью другой функции.

Я решил это с помощью другой функции, но хотелось бы, чтобы было другое хорошее решение.

Интересно, в чем заключалась фундаментальная проблема?Это просто другая альтернатива.

    @available(iOS 10, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let data = response.notification.request.content.userInfo
        guard
            let aps = data[AnyHashable("aps")] as? NSDictionary,
            let alert = aps["alert"] as? NSDictionary,
            let body = alert["body"] as? String
            else {
                Log.Error("it's not good data")
                return
        }


        Log.Info(body)

        completionHandler()
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...