Reputation: 599
I have an app. It uses FCM to push notifications. The json of messages do look like:
{ "to": "xxx", "notification" : {
"body" : "Hi",
"badge" : 1,
"sound" : "default"
},
"data" : {
"id" : "xxx",
"first_name" : "xxx",
"last_name" : "xxx",
"full_name" : "xxx",
"primary_image" : "xxx",
"matchid" : "xxx",
"type": "match"/"message"
},
"content_available": true,
"priority": "high"
}
I have a "type" in data to detect which screen will launched when touch my notifications. If type == "match" -> go to MatchVC, and type == "message" -> go to MessageVC. I have an issue that if my app is in foreground I can reach the data from didReceiveRemoteNotification:userinfo
then I can detect the push screen, however if my app is background or close, I only get the notification without data from didReceiveRemoteNotification:userinfo
. And when I click the notifications, it just opens my app. Any solutions are appreciated.
Upvotes: 2
Views: 2012
Reputation: 1177
In Firebase iOS sdk, you would have the following code snippet in app delegate.
Notice there are 2 userNotificationCenter methods. First one will get called when the app is foreground. 2nd will get called when you tap on the push notification from the tray.
Full code can be found from Firebase Official Github repository (iOS Quickstart). https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift
@available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print("userInfo 1st")
print(userInfo)
let id = userInfo["id"]
let firstName = userInfo["first_name"]
print(id ?? "")
print(firstName ?? "")
// Change this to your preferred presentation option
completionHandler([])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print("userInfo 2nd")
print(userInfo)
let id = userInfo["id"]
let firstName = userInfo["first_name"]
print(id ?? "")
print(firstName ?? "")
completionHandler()
}
}
Upvotes: 1