3

I am having a great trouble with Notification and pending Intent. I am trying to open Chat activity with appropriate user_details from which the message is sent. That's why on Firebase Function I have passed the from_user_id which is the one who is sending the message. I am getting correct logs there in FCM but when I receive a chat notification and open it It opens activity without any userName and messages. It open a new instance of the activity with default values.

  @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);


    String notification_title = remoteMessage.getNotification().getTitle();
    String notification_message = remoteMessage.getNotification().getBody();

    String click_action = remoteMessage.getNotification().getClickAction();


    String from_user_id = remoteMessage.getData().get("from_user_id");

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.chitchat_icon)
                        .setContentTitle(notification_title)
                        .setAutoCancel(true)
                        .setContentText(notification_message);


        Intent resultIntent = new Intent(click_action);
        resultIntent.putExtra("user_id", from_user_id);


        PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

        mBuilder.setContentIntent(resultPendingIntent);


    int mNotificationId = (int) System.currentTimeMillis();

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mNotificationManager.createNotificationChannel(mChannel);
        mBuilder.setChannelId(CHANNEL_ID);
    }

    mNotificationManager.notify(mNotificationId,mBuilder.build());
}

Message payload:

  const payload = {
     notification: {
      title: userName,
      body: message,
      icon: "default",
      click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
     },
     data : {
      from_user_id : from_user_id
     }
    };

My Manifest look like this:

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.chitchat">
...        
       <application
            android:name=".ChitChat"
            android:allowBackup="true"
            android:icon="@drawable/chitchat_icon"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".GroupChatActivity"></activity>
            <activity android:name=".CallActivity" />
            <activity
                android:name=".ChatActivity"
                android:parentActivityName=".MainActivity">
                <intent-filter>
                    <action android:name="com.example.chitchat_TARGET_MESSAGE_NOTIFICATION" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity android:name=".ProfileActivity">
                <intent-filter>
                    <action android:name="com.example.chitchat_TARGET_NOTIFICATION" />

                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity
           ...
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
                android:theme="@style/Base.Theme.AppCompat" />

            <meta-data
                android:name="com.google.firebase.default_notification_channel_id"
                android:value="fcm_default_channel" /> <!-- adding -->
            <service android:name=".FirebaseMessagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
        </application>
        ...

    </manifest>

I don't know If I have added some different functions like LifeCycleEvent Listners and EmailVerification for registration has created issues. I am also unable to log the problem I don't know why. Please, appropriate suggestions. Thanks

Rehan
  • 454
  • 7
  • 19

1 Answers1

2

First of all change resultIntent.putExtra inside onMessageReceived like:

resultIntent.putExtra("from_user_id", from_user_id);

And in your ChatActivity fetch user_id like below:

String user_id = getIntent().getStringExtra("from_user_id") 

Hope this solve your problem.

Or change your notification payload:

const payload = {
     notification: {
      title: userName,
      body: message,
      icon: "default",
      click_action : "com.example.chitchat_TARGET_MESSAGE_NOTIFICATION"
     },
     data : {
      user_id : from_user_id
     }
    };

And inside onMessageReceived change:

String from_user_id = remoteMessage.getData().get("from_user_id");

To

String from_user_id = remoteMessage.getData().get("user_id");

Reason: During background, System generates the notification without executing your code inside onMessageReceived. That's why it puts the extra as it gets from notification payload which is from_user_id, that's you send from server.

Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46