Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to send specific link inside notifications send through firebase push notification,how to do it?

Suppose in my notification,Today is X's birthday it opens FB link of X on clicking the notification and same for Y, How to do it using firebase

like image 477
Ankit Avishek Avatar asked Feb 05 '23 11:02

Ankit Avishek


1 Answers

Below is the way to do that:

1) server site make request with:

Request URL: https://fcm.googleapis.com/fcm/send

Method: POST

Header:

  • Content-Type: application/json

  • Authorization: key=...your_key...

Body:

{
  "registration_ids" :[devices_token],
  "priority" : "high",
  "notification" : {
    "body" : "Notification body here",
    "title" : "notification title here",
    "click_action": "action.open.facebook.with.url"
  },
  "data" : {
    "openURL" : "https://facebook.com/xxx"
  }
}

2) Android app:

AndroidManifest.xml:

<activity android:name=".activity.OpenFacebookLink">
    <intent-filter>
        <action android:name="action.open.facebook.with.url" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

In your subclass of FirebaseMessagingService:

@Override
public void onMessageReceived(RemoteMessage message) {
    super.onMessageReceived(message);
    String url = message.getData().get("openURL");
    if(message.getNotification()!=null) {
        // do some thing with url when app in foreground
    } else {
        // do some thing with url when app in background
    }
}

In your OpenFacebookLink activity let do that:

Intent intent = getIntent();
String url = intent.getStringExtra("openURL");
// do open facebook with url
like image 70
Robust Avatar answered Feb 08 '23 15:02

Robust