2

I am using FCM for push notification and i am sending data from Notification to the activity which i am opening.

Code for building Notification:

private void handleNotification(RemoteMessage remoteMessage) {
    String notTitle = remoteMessage.getNotification().getTitle();
    String notBody = remoteMessage.getNotification().getBody();

    Intent resultIntent = new Intent(this, HomeActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    resultIntent.putExtra("pushNotClick", "yes");
    resultIntent.putExtra("pushNotHead", ""+notTitle);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setSmallIcon(R.drawable.fb_icon);
    mBuilder.setColor(getResources().getColor(R.color.colorPrimary));
    mBuilder.setContentTitle(notBody)
            .setContentText(notTitle)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        assert mNotificationManager != null;
        mBuilder.setSmallIcon(R.mipmap.icon_not);
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify((int) System.currentTimeMillis() /* Request Code */, mBuilder.build());
}

And i am getting the intent extras in my activity like this:

String notState = getIntent().getStringExtra("pushNotClick");
String notHead = getIntent().getStringExtra("pushNotHead");

But the problem is, everytime the Intent Extras are null in the Activity, i have checked all the possible reasons i found here in the community, but everytime the response is same.

I have tried below mentioned links

Intent's extra is always null

Android Notification PendingIntent Extras null

Always getting data null from notification intent android

I am not sure where i am doing wrong.

Kunal
  • 412
  • 5
  • 21

4 Answers4

2

I even after lots of debugging and all, couldn't find the reason of the problem. Lastly i changed the way of sending Intent extras to the activity. Now i am using bundle to send the data and its working like a charm. This is a way round and not an answer to the question, so i won't accept it as an accepted answer.

Kunal
  • 412
  • 5
  • 21
0

Have you tried checking RemoteMessage data field? Something like this:

// Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }
cobe_tech
  • 176
  • 6
  • Yes, data is coming inside RemoteMessage. Not like this but data is coming in this field. – Kunal Jul 19 '19 at 05:53
-1

Are you checking in onCreate? If so, check on onNewIntent.

Davis
  • 137
  • 1
  • 6
  • how does this help? – Zun Jul 18 '19 at 12:06
  • In fact, it may help, if the activity is running already, the intent should be checked in "onNewIntent". Anyway he should describe the answer in details. – barotia Jul 18 '19 at 12:20
  • Override the onNewIntent activity method. You receive a intent object here which you can get extras. – Davis Jul 18 '19 at 12:31
  • And what if the activity is not running? For example, the cloud message is passed in the notification tray while the app is closed down? – Zun Jul 18 '19 at 12:39
  • According to your code, the activity will be launched after clicking the notification and starts the activity lifecycle. That's how PendingIntent works. – Davis Jul 18 '19 at 12:46
-2

If "notTitle" is integer than send it with Intent like this

 resultIntent.putExtra("pushNotHead", notTitle.toString);
sohel.eco
  • 337
  • 2
  • 12