Firebase iOS Receive Data from Push Notification

Firebase iOS receive data from push notification

payload like this

{
"aps" : {
"alert" : "Notification with custom payload!",
"badge" : 1,
"content-available" : 1
},
"data" :{
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
}
}

Read the payload data

@available(iOS 10, *)

extension AppDelegate : UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo

if let aps = userInfo["aps"] as? NSDictionary
{
let alert = aps["alert"]as? NSString
let badge = aps["badge"] as? Int
}
completionHandler([.alert, .badge, .sound])

}

How to get data from Firebase FCM push notification in iOS

The data should be available directly on the userInfo dictionary, not on the "aps" key and you should be able to access it like this:

let customValue = userInfo["customKey"] as? String

As stated on the Firebase documentation,

The payload of notification messages is a dictionary of keys and values. Notification messages sent through APNs follow the APNs payload format as below:

{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}

IOS data notifications with FCM

From what I understand so far, there is no way of solving this issue properly on the ios side. It works perfectly on the android side because the application is awoken in all states (foreground, background & closed).

You have two kinds of messages you can send:

A notification-message which is displayed and handled by the operating system directly.

A data-message which is handled by the application.

If you add a custom tag, it now becomes a data-message and would have to be handled by the application. You can add a content_available tag in data-message to let the application know about the message but the problem is that the data-message is only delivered to the application in ios if the application is in the foregrounded (opened) or in the background (minimised). The data-message will not be delivered to the application if the user has "force-closed" the application (even with the background notifications enabled).

Solution is to handle the intended user on the server side and solve the multiple-users to one-device-token problem by maintaining the a one to one device-token to user relationship.

How to get push notification custom data from Firebase when app is terminated

If you app startup from FCM message click when your app terminated, you need to custom this

FirebaseMessaging.instance.getInitialMessage().then((message) { //do something })

How to get data from notification when receiving push for android (firebase)?

After more time I solved this problem

In my Manifest.xlm I added this

<intent-filter>
<action android:name="VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

(for my MainActivity)

In MainActivity in method onCreate added

Intent intent = getIntent();
Bundle data = intent.getExtras();
Uri deepLinkUri = null;
if (data.containsKey("deeplink"))
deepLinkUri = Uri.parse(intent.getExtras().getString("deeplink"));

And POST JSON

{
"to": "dmi38i.....yVVvs",
"data": {
"deeplink": "app://webview/google.com"
},
"notification": {
"title": "test_notification",
"body": "test_notification",
"click_action" : "VIEW"
},
"priority": "high"
}

(Added "click_action" : "VIEW" to notification)

I hope this helps someone



Related Topics



Leave a reply



Submit