Solution:
- I have to add intent-filter in an activity in manifest file to which I want to go, on-tapping of push notification. This notification will have some url which is being called deeplink in Android Terminology. You can refer below link for more about deeplink.
https://developer.android.com/training/app-links/deep-linking
I was using these two links as a deeplink: "www.somedomain.com/about" & "www.somedomain.com/app".
Please do not add http or https in intent-filter, they are not supported. Chekout this conversation for more clarification. I'm putting an image of that chat as well, If in case in future the link gets expire.
- Please refer below code for how I'm passing deeplink to NotificationManager. intent-filter aumatically intercept and launch that particular activity.
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String message = data.get("message");
String deepLink = data.get("deepLink");
Notification notification = new Notification();
notification.setTitle(title);
notification.setMessage(message);
notification.setDeepLink(deepLink);
sendNotification(this, title, message, deepLink);
}
public static void sendNotification(Context context, String title, String message, String deepLink) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel notificationChannel = new NotificationChannel("any_default_id", "any_channel_name",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Any description can be given!");
notificationManager.createNotificationChannel(notificationChannel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(android.app.Notification.PRIORITY_MAX)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(deepLink));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notificationBuilder
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notificationBuilder.build());
}
}
AndroidManifest.xml
<activity
android:name=".mvp.view.activity.ActivityName"
android:label="@string/title_activity_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.somedomain.com"
android:path="/about"
android:scheme="app" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.somedomain.com"
android:path="/contact"
android:scheme="app" />
</intent-filter>
</activity>
Extra:
If you want to receive some more data(i.e userId or loanId) in that activity, you can pass it to while sending push notification from your server(i.e back-end or web based dashboard). You can do like below.
{
"data": {
"userId": "65431214564651251456",
"deepLink": "www.somedomain.com/app",
"title": "This is title!",
"message": "This is message!"
},
"to": "FCM token here"
}
Important: Below JSON will not work, this is for reference only. This is also nowhere mentioned in documentation. So kindly take care of it. Correct JSON is above.
{
"to": "FCM Token here",
"notification": {
"Body": "This week’s edition is now available.",
"title": "NewsMagazine.com",
"icon": "new"
},
"data": {
"title": "This is title!",
"message": "This is message!"
}
}
- You can receive extra data(i.e userId or loanId) in method onMessageReceived of MyFirebaseMessagingService class like below.
String userId = data.get("userId");
intent.putExtra(Intent.EXTRA_TEXT, userId);
- And in that activity you can write like below in onCreate method.
Intent intent = getIntent();
if (intent != null) {
String intentStringExtra = intent.getStringExtra(Intent.EXTRA_TEXT);
if (intentStringExtra != null) {
userId = intentStringExtra;
}
}
<data android:host="www.somedomain.com" android:path="/contact" android:scheme="app" />
we use android:scheme="app" for open specific activity and android:scheme="https" for specific url. refer that link which you have attached. – Piles