2

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

Custom data firebase dashboard

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.

elchicho44
  • 138
  • 7
  • Please post the example of the notification that you are sending – Petr Kubáč Nov 24 '20 at 17:22
  • Please show your manifest file where you have defined the activity where you want to send your user to. Or just post the whole manifest if it ain't too big – Sarah Khan Nov 24 '20 at 17:25
  • @Kubicko I have attached the screenshot. Please check. – elchicho44 Nov 24 '20 at 20:34
  • @SarahKhan I have added the snippet of that class in manifest. Kindly check. – elchicho44 Nov 24 '20 at 20:35
  • You have all your host and schemes written in MainActivity's intent filter, so only MainActivity will open. – Sarah Khan Nov 24 '20 at 20:44
  • Tell me which activity you want to navigate to and send the deeplink associated with it – Sarah Khan Nov 24 '20 at 20:46
  • @SarahKhan I use two deeplink (Removed the other intent filter for simplification) , id is for Class A while qid is for Class B activity. After authenticating the user, depending upon the key (ie. id, qid(for other type)) the main activity redirects to the respective activity where query parameter displays the associated data from db. – elchicho44 Nov 24 '20 at 20:54
  • 1
    Okay, so how do you extract the host in MainActivity? You mind publishing the code where in your MainActivity are you receiving the url send through deeplink? Or can you just post the whole MainActivity code? – Sarah Khan Nov 25 '20 at 03:17

1 Answers1

1

A little context:I use two deeplink (Removed the other intent filter for simplification) , id is for Class A while qid is for Class B activity. After authenticating the user, depending upon the key (ie. id, qid(for other type)) the main activity redirects to the respective activity where query parameter displays the associated data from db.

I just learned that onMessageReceived is called only when app is in foreground, while when the app is in background the data is passed as intent extra. So in the main activity, you can get the key value custom when app is in background as follows:

getIntent().getExtras().getString("key");

Instead of dynamic link, I pass the query parameter as the value of custom data, and then process it like I process deeplink paramater. Also, the above mentioned declaration of NotificationBuilder is deprecated and hence, notification wasn't showing when app was in foreground. Change it to this.

NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)

Now to manage notification data when app is foreground, pass the remotemessage to the main activity as an intent extra through pendingintent and process it like the case of when app is in background. Replace

        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(deepLink));
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

With this

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
intent.putExtra("remoteMessage", remoteMessage);
elchicho44
  • 138
  • 7