-1

I'm trying to send the click-action data with the notification to handle the onClick event for it but unfortunately the app receives no data through the onMessageReceived(). I have tried plenty of things but all in vain.

Here is my code:

@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");
    Log.v("user id", from_user_id);
    if(from_user_id!= null){
        Toast.makeText(this, from_user_id, Toast.LENGTH_SHORT).show();
    }
    NotificationCompat.Builder mBuilder = new 
NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(notification_title)
            .setContentText(notification_message)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    Intent resultIntent = new Intent(click_action);
    resultIntent.putExtra("user_id", from_user_id);
    Log.v("user id", from_user_id);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, 
PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

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

    NotificationManager mNotifyManger = 
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    mNotifyManger.notify(mNotificationID, mBuilder.build());

    }

and here is my mainfest:

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

and this is my gradle:

implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-storage:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.firebaseui:firebase-ui-database:4.2.1'
implementation 'com.google.firebase:firebase-messaging:17.3.4'

Edited: Here is the data I'm expecting:

const deviceToken = 
admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
if (!result || !result.exists){throw new Error("Profile doesn't exist")}
const token_id = result.val();
const payload = {
    notification: {
        title: "New Friend Request",
        body: `${userName} has sent you a Friend Request`,
        icon: "default",
        click_action: "com.example.alaa.lapitchatapp.Target_Notification"

    },
    data:{
        from_user: from_user_id
    }
};
halfer
  • 19,824
  • 17
  • 99
  • 186
Alaa Emad
  • 1
  • 5

4 Answers4

1

If your RemoteMessage contains notification key onMessageReceived is only called if your app in background.

Bek
  • 7,790
  • 4
  • 18
  • 31
  • I'm already sending data plz check the edit I've made to my question, I thought the problem was with my JavaScript code so I was trying to log the remote message to see if it is coming. and yes my app is in the background – Alaa Emad Nov 07 '18 at 04:05
0

Move notification part into data. If you have notification setup from the request, FCM uses system notification tray to display the notification you wouldn't have any control over it, On the other hand if you need to process the push response, You should avoid using the notification part in the request

const payload = {
  data:{
    from_user: from_user_id,
    title: "New Friend Request",
    body: `${userName} has sent you a Friend Request`,
    icon: "default",
    click_action: "com.example.alaa.lapitchatapp.Target_Notification"
  }
};
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
0

Firstly, are you passing your device's firebase ID in the payload? If yes, Please update your payload body mentioned. I don't see it there. Your payload must have the firebase ID something like this :

"registration_ids":[
  "your device's firebase ID"
]

or

"to":"your device's firebase ID"

or

"token":"your device's firebase ID"

Secondly, I don't see the point of this code :

if (true) {
        Log.d(TAG, "Message data payload: there is not a problem");
    } else {
        // Handle message within 10 seconds
     Log.d(TAG, "Message data payload: there is a problem");
    }

What is the point of an else for an if statement which is always true?

Thirdly, the click action value you have given in the payload - com.example.alaa.lapitchatapp.Target_Notification Is this a class you are trying to open on click of the notification?

Derryl Thomas
  • 1,324
  • 12
  • 22
  • I've commented the real code and just tried to see if the method can be called by using some logs and it never called. and yes this is a real class I'm trying to open when i click on the notification. I did not know about this id you mentioned. do you think this maybe the problem? – Alaa Emad Nov 07 '18 at 13:00
  • Here is the code I'm actually using to get the data, plz check the edit. thanks in advance – Alaa Emad Nov 07 '18 at 13:17
  • Yes, I think the missing ID(or token) might be the issue. Without any given token or topic, who are you trying to send the message or notification to? A token or device ID is A registration token that identifies the recipient device of the message. This determines which device the message is sent to. Please refer firebase documentation - https://firebase.google.com/docs/cloud-messaging/android/first-message and https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Notification – Derryl Thomas Nov 07 '18 at 20:56
  • Also, if you want to open an activity using click_action, then simply mentioning the class name will not cause it to open. You will have to add click_action in the payload and an intent filter in the app to handle it. Kindly refer https://stackoverflow.com/questions/37407366/firebase-fcm-notifications-click-action-payload and https://medium.com/@Miqubel/mastering-firebase-notifications-36a3ffe57c41 for more info. – Derryl Thomas Nov 07 '18 at 21:25
  • I'm already passing the token id but in another place, the problem now that the service is called for only one time then when i try to recall it when sending other notifications nothing is happening. And yes I'm adding click_action with the referred activity and using an intent to receive it and have the action to open the activity. But nothing is happening too. Maybe it's something to do with the firebase as it only called once? I don't know. @DerrylThomas – Alaa Emad Nov 07 '18 at 22:38
  • So you can send the notification once to your device and you receive it as expected and the following requests you send don't reach the device? Was this always the case? Or did you make any change now which got this to start working? Also, when you say you try to recall sending other notifications, do you use the same payload and send it again? Or there are some changes in your payload? – Derryl Thomas Nov 08 '18 at 08:40
  • Nothing has changed. I'm creating a notification builder in my onMessegeReceived method to know if the method is called or not, but i don't know what's going wrong the method has been called once after that nothing is happening like if there isn't any service. @DerrylThomas – Alaa Emad Nov 08 '18 at 11:01
  • This is weird. If it is getting called once, should work again too. Here are a couple of things I would recheck to be sure: 1. When you push your second notification, please recheck if your app is in the background. As @Bek mentioned, this might be a problem. 2. Recheck whether your device token(firebase instance ID) had changed in between. This shouldn't usually happen. However, recheck to be certain as the firebase token changes in some situations - https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId – Derryl Thomas Nov 09 '18 at 09:10
0

I've found the answer to my question after too much digging in the firebase documentation.

You'll find it there: Firebase Message Service not called but one time only

Thank you All.

Alaa Emad
  • 1
  • 5