TobiasBuchholz / Plugin.Firebase

Wrapper around the native Android and iOS Firebase Xamarin SDKs
MIT License
211 stars 49 forks source link

Is it possible to create Push Notifications with deep linking? #196

Closed play4uman closed 1 year ago

play4uman commented 1 year ago

Suppose that we want to implement a functionality like Facebook Messenger where clicking on the notification for a received chat message would open the app on the exact chat page that the notification was received, i.e. "deep linking" when opening the notifications. Is this possible with the current implementation?

According to this SO reply it's possible by setting the "Link" property in the "Notification" object using the firebase REST SDK but I was unable to test it since I'm using the Firebase Admin SDK which doesn't have a "Link" property under the "Notification" object.

https://fcm.googleapis.com/fcm/send
Content-Type: application/json
Authorization: key={SERVER_KEY}

{
 "to" : "{Firebase client token}",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
     "link": "example://my.app/products"  <<-- Here is the solution
 }
TobiasBuchholz commented 1 year ago

Yes, you can put whatever additional field you like into the data object of a push notification and extract it from the FCMNotification data property. So in your case you could send the notification like this:

https://fcm.googleapis.com/fcm/send
Content-Type: application/json
Authorization: key={SERVER_KEY}

{
 "to" : "{Firebase client token}",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
  },
  "data" : {
     "link": "example://my.app/products"  <<-- Here is the solution
  }
}

Extract the link from FCMNotification like this:

private void HandleNotification(FCMNotification notification)
{
    var link = notification.Data["link"];
    // navigate to link's destination
}
play4uman commented 1 year ago

@TobiasBuchholz Thank you that works perfectly!