I have been trying to implement fcm notification with deep linking. I have followed this post. This Post I send notification using the Cloud Messaging Dashboard with custom data as "deepLink" key and it's value but I'm not able to retrieve any remoteData , I tried to log.d it but nothing appears. So, whenever I send notification, it appears but leads to the main activity, not the desired deep linked activity. Please help.
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
Map<String, String> data = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String title = data.get("title");
String message = data.get("message");
String deepLink = data.get("deepLink");
Log.d(TAG,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("xyz01", "xyz",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.drawable.ic_stat_notifylogo)
.setPriority(android.app.Notification.PRIORITY_MAX)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_notifylogo));
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());
}
@Override
public void onNewToken(String token) {
sendRegistrationToServer(token);
}
private void handleNow() {
//Log.d(TAG, "Short lived task is done.");
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
Manifest of the activity where I want to send the user(/where dynamic link is handled).
<activity android:name="c.testnotification.app.MainActivity" android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</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="https://somedomainxyz.com/?id="
android:scheme="https"/>
<data
android:host="https://somedomainxyz.com/?id="
android:scheme="http"/>
</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="somedomainxyz.com"
android:path="/?id="
android:scheme="app" />
</intent-filter>
</activity>
That's how I add custom data through dashbboard
I tried sending through other ways like complete deeplink url instead of the short, http,https, etc. But still didn't work.
Also is this approach enough to manage both situation, ie. when the app is in foreground and background.