0

I am using a webview application and want to load the url which received in notification. For that I am using putExtra to receive the url in main activity but I am getting null.

Here is my code FcmMessagingService code -

public class FcmMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra("fcm_url", "here url will be there");
        PendingIntent pendingIntent =  PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Message");
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("Message", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(0, notificationBuilder.build());
    }
}

This is my MainActivity onCreate code -

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    final String fcm_message = intent.getStringExtra("fcm_url");

    if(fcm_message!=null){
        Log.d("Fcm_url",fcm_message);
    }
    else{
        Log.d("Fcm_url","This is null");
    } 
    ...

When I open app directly logcat prints This is null which is fine but when I send a notification and open the app by clicking notification, it again print This is null, what I am doing wrong ?

Update

When I add Log.d.. inside my onMessageRecieved there is no execution of it see

public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("on_message_recieved","fcm message recieved");
        ....

but it is not printing after receiving notification ???

In my ManiFest I had added this -

<service
            android:name=".FcmMessagingService"
            android:permission="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

It seems that it is not executing my custom FcmMessagingService but why ??

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50

4 Answers4

0

Getting value from FCM to MainActivity try below code.

if (getIntent().getExtras() != null) {
          String fcm_url=  getIntent().getExtras().getString("fcm_url")
        } 
  • I had already tried this, and also after your answer I tried again, but all the time `getIntent().getExtras()` is `null` , any other suggestions ? – Pankaj Sharma Jan 25 '20 at 10:04
0

Remove this link and try.

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
0

In your FcmMessagingService class add the following code:

    Intent intent = new Intent(context, MainActivity.class);

    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra("myUrl", deepLink);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notificationBuilder.setContentIntent(pendingIntent);

Then in MainActivity class:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    ............
    
    Intent intent = this.getIntent();

    Bundle extras = intent.getExtras();
    if(extras != null){
        Url = intent.getStringExtra("myUrl");
            Log.w("myTag", Url);
    } else {
        Log.w("myTag", "extras is null");
    }

    ............
    }
CHIRAG SAHU
  • 64
  • 1
  • 2
  • 9
  • Seems that the OPs issue was that onMessageRecieved is not being called. Does your code fix that? Also, where in MainActiivity should your second block of code go? – Liron Jul 08 '21 at 07:50
  • Hello, I ran the OP's code on my device and it seems to be running fine on my system. and the MainActivity code should be in OnCreate method, I have updated the code – CHIRAG SAHU Jul 08 '21 at 19:38
0

I have faced the same issue and I have found one solution for it. I don't know it is 100% proper for you or not. You have to replace a couple of lines in your code and it will work perfectly.

intent.putExtra("fcm_url", "here url will be there")

instead of this just change as below

intent.setData(Uri.parse("here url will be there"))

Change in get data in the activity as below:

 final String fcm_message = intent.getStringExtra("fcm_url");

replace this with below

final String fcm_message = intent.data.toString()

This is working properly for me. Also, If you want to pass multiple data, you can create a class with fields, convert class data into the string using GSON and pass as a string with setData.

I hope, this will useful for you to solve your issue.

Happy coding...

Pratik Satani
  • 1,115
  • 1
  • 9
  • 25