Skip to content

Commit bb6af00

Browse files
authored
Log incoming notifications to console as JSON (firebase#286)
This will help debug incoming notifications.
1 parent 533d647 commit bb6af00

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Example/Messaging/App/iOS/AppDelegate.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9696
NotificationCenter.default.post(name: UserNotificationsChangedNotification, object: nil)
9797
}
9898

99+
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
100+
print("application:didReceiveRemoteNotification:fetchCompletionHandler: called, with notification:")
101+
print("\(userInfo.jsonString ?? "{}")")
102+
completionHandler(.newData)
103+
}
104+
99105
func applicationDidBecomeActive(_ application: UIApplication) {
100106
// If the app didn't start property due to an invalid GoogleService-Info.plist file, show an
101107
// alert to the developer.
@@ -119,9 +125,8 @@ extension AppDelegate: MessagingDelegate {
119125
// arrive.
120126
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
121127
// Convert to pretty-print JSON
122-
guard let data =
123-
try? JSONSerialization.data(withJSONObject: remoteMessage.appData, options: .prettyPrinted),
124-
let prettyPrinted = String(data: data, encoding: .utf8) else {
128+
guard let prettyPrinted = remoteMessage.appData.jsonString else {
129+
print("Received direct channel message, but could not parse as JSON: \(remoteMessage.appData)")
125130
return
126131
}
127132
print("Received direct channel message:\n\(prettyPrinted)")
@@ -136,4 +141,15 @@ extension AppDelegate {
136141
func onMessagingDirectChannelStateChanged(_ notification: Notification) {
137142
print("FCM Direct Channel Established: \(Messaging.messaging().isDirectChannelEstablished)")
138143
}
144+
}
145+
146+
extension Dictionary {
147+
/// Utility method for printing Dictionaries as pretty-printed JSON.
148+
var jsonString: String? {
149+
if let jsonData = try? JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted]),
150+
let jsonString = String(data: jsonData, encoding: .utf8) {
151+
return jsonString
152+
}
153+
return nil
154+
}
139155
}

Example/Messaging/App/iOS/NotificationsController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ extension NotificationsController: UNUserNotificationCenterDelegate {
127127
withCompletionHandler completionHandler:
128128
@escaping (UNNotificationPresentationOptions) -> Void) {
129129
// Always show the incoming notification, even if the app is in foreground
130+
print("Received notification in foreground:")
131+
let jsonString = notification.request.content.userInfo.jsonString ?? "{}"
132+
print("\(jsonString)")
130133
completionHandler([.alert, .badge, .sound])
131134
}
132135
}

0 commit comments

Comments
 (0)