Reputation: 91
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
Upvotes: 8
Views: 22951
Reputation: 2485
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
Upvotes: 9
Reputation: 674
If you want to send the notification manually from FCM panel then: then make a key like: link and put the value i.e. your fb link. Then send the notification to that particular user or a topic or a segment from FCM panel.
You can handle it inside your application like:
Extract the data from notification then...
if (data != null) {
directLink = data.optString("directLink");
}
Inside the notification open method:
Intent browserIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(link));
browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(browserIntent);
But if you want to make this process automated then you'll have to write a script that will extract the birthday links from some database and send notifications to users via FCM API and do a post request on that API.
Upvotes: 0