@@ -96,6 +96,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
96
96
NotificationCenter . default. post ( name: UserNotificationsChangedNotification, object: nil )
97
97
}
98
98
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
+
99
105
func applicationDidBecomeActive( _ application: UIApplication ) {
100
106
// If the app didn't start property due to an invalid GoogleService-Info.plist file, show an
101
107
// alert to the developer.
@@ -119,9 +125,8 @@ extension AppDelegate: MessagingDelegate {
119
125
// arrive.
120
126
func messaging( _ messaging: Messaging , didReceive remoteMessage: MessagingRemoteMessage ) {
121
127
// 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) " )
125
130
return
126
131
}
127
132
print ( " Received direct channel message: \n \( prettyPrinted) " )
@@ -136,4 +141,15 @@ extension AppDelegate {
136
141
func onMessagingDirectChannelStateChanged( _ notification: Notification ) {
137
142
print ( " FCM Direct Channel Established: \( Messaging . messaging ( ) . isDirectChannelEstablished) " )
138
143
}
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
+ }
139
155
}
0 commit comments