diff options
author | Riz <rsattar@gmail.com> | 2017-09-19 14:28:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-19 14:28:06 -0700 |
commit | 637bf879adc17b9609175499ed0b12e51b64ef7b (patch) | |
tree | 6c78a2261c4e6d53571f3e762cca7c7a3e8accc0 /Example/Messaging/App | |
parent | f42d6d11e4fa9429ffa7d936b7f782f15eb3bb86 (diff) |
Log incoming notifications to console as JSON (#286)
This will help debug incoming notifications.
Diffstat (limited to 'Example/Messaging/App')
-rw-r--r-- | Example/Messaging/App/iOS/AppDelegate.swift | 22 | ||||
-rw-r--r-- | Example/Messaging/App/iOS/NotificationsController.swift | 3 |
2 files changed, 22 insertions, 3 deletions
diff --git a/Example/Messaging/App/iOS/AppDelegate.swift b/Example/Messaging/App/iOS/AppDelegate.swift index 4f8e504..6d47ded 100644 --- a/Example/Messaging/App/iOS/AppDelegate.swift +++ b/Example/Messaging/App/iOS/AppDelegate.swift @@ -96,6 +96,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { NotificationCenter.default.post(name: UserNotificationsChangedNotification, object: nil) } + func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { + print("application:didReceiveRemoteNotification:fetchCompletionHandler: called, with notification:") + print("\(userInfo.jsonString ?? "{}")") + completionHandler(.newData) + } + func applicationDidBecomeActive(_ application: UIApplication) { // If the app didn't start property due to an invalid GoogleService-Info.plist file, show an // alert to the developer. @@ -119,9 +125,8 @@ extension AppDelegate: MessagingDelegate { // arrive. func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { // Convert to pretty-print JSON - guard let data = - try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted), - let prettyPrinted = String(data: data, encoding: .utf8) else { + guard let prettyPrinted = remoteMessage.appData.jsonString else { + print("Received direct channel message, but could not parse as JSON: \(remoteMessage.appData)") return } print("Received direct channel message:\n\(prettyPrinted)") @@ -136,4 +141,15 @@ extension AppDelegate { func onMessagingDirectChannelStateChanged(_ notification: Notification) { print("FCM Direct Channel Established: \(Messaging.messaging().isDirectChannelEstablished)") } +} + +extension Dictionary { + /// Utility method for printing Dictionaries as pretty-printed JSON. + var jsonString: String? { + if let jsonData = try? JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted]), + let jsonString = String(data: jsonData, encoding: .utf8) { + return jsonString + } + return nil + } } diff --git a/Example/Messaging/App/iOS/NotificationsController.swift b/Example/Messaging/App/iOS/NotificationsController.swift index 7484d07..a6b1544 100644 --- a/Example/Messaging/App/iOS/NotificationsController.swift +++ b/Example/Messaging/App/iOS/NotificationsController.swift @@ -127,6 +127,9 @@ extension NotificationsController: UNUserNotificationCenterDelegate { withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { // Always show the incoming notification, even if the app is in foreground + print("Received notification in foreground:") + let jsonString = notification.request.content.userInfo.jsonString ?? "{}" + print("\(jsonString)") completionHandler([.alert, .badge, .sound]) } } |