1

I'm sending notifications from my Flask server using PyFCM, and I'm testing it on a single Android device. The test is like this: I am signed in as user A, and I make a comment on a post of user B which should display a push notification once B signs in. Here is how I send the notification from my server:

registration_id="<device_registration_id>"
message_body = "A has commented on your post."
data_message = {"sender": current_user.id}
result =  push_service.notify_single_device(
    registration_id=registration_id,
    message_body=message_body,
    data_message=data_message
)

And this is how I receive the message in the Android's Firebase messaging service:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent resultIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT):

    String senderId = remoteMessage.getData().get("sender");
    if (senderId != currentUser.id) {
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this, "default_channel")
            .setSmallIcon(R.drawable.android_icon)
            .setContentTitle("New Comment")
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(soundURI)
            .setContentIntent(resultIntent);

        NoticationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, mNotificationBuilder.build());
    }  
}

As you can see, I have this condition: senderId != currentUser.id before actually composing the notification. It's because I'm using one device to send and receive the notification so there's only one registation_id/token for both users A and B. If I remove that condition, user A will receive the notification right after commenting on B's post. I want to ensure that B is the one who receives the notification. However, after logging out as A and logging in as B, I couldn't see any push notification.

Bargain23
  • 1,863
  • 3
  • 29
  • 50
  • Hi Bargain23. I'm not quite sure where the issue is here. Are you not receiving the message at all (`onMessageReceived` not triggered) or is it that the message is received and for some reason, it is not passing your `senderId != currentUser.id` condition? – AL. Apr 27 '18 at 05:17
  • @AL. Actually, `senderId != currentUser.id` condition was put in place to make sure that no two users of the same device will receive the notification. – Bargain23 Apr 27 '18 at 05:23
  • Err. I get that part. What I don't understand is are you able to trigger `onMessageReceived` or not? – AL. Apr 27 '18 at 05:24
  • @AL. I think onMessageReceived is only being triggered once. Say, I'm trying to send a notification from my server to all users, and I receive it while logged in as A. I clear the notification tray, log out, and log in as B, but I don't see another instance of the notification. – Bargain23 Apr 27 '18 at 05:28

1 Answers1

1

I think onMessageReceived is only being triggered once. Say, I'm trying to send a notification from my server to all users, and I receive it while logged in as A. I clear the notification tray, log out, and log in as B, but I don't see another instance of the notification.

This is working as intended. From your post, I assume that you are not specifically deleting the registration token i.e. users of the same device re-use the same token. So the flow now looks like this:

  1. User A signs in, so deviceToken = A.
  2. You send a message from your server to deviceToken.
  3. deviceToken receives the message, but you do not display it.
  4. User A signs out, User B signs in, now deviceToken = B. Note: Same deviceToken, just different user.

In step 4, you're still expecting a message to arrive, but technically, it already did when User A was still signed in. onMessageReceived won't trigger again since it already received the message as expected.

In order to test your desired behavior, you would need two devices. I'm also actually doing this to an app I made (checking the senderId with the currentUser id), so I think it should work as well for you.

Also, the usual and suggested flow with FCM when signing out is you have to invalidate the token -- see my answer here for more details.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • Thank you for this advice. One thing, though, if I invalidate a device token when a user has logged out, and during this period, how will I be able to send him a notification knowing that his token has been invalidated? – Bargain23 Apr 27 '18 at 17:23
  • After the token has been invalidated, `onTokenRefresh()` should trigger, wherein you could call `getToken()` again and store that new token to your server. The token however, will not be associated to a specific user, until such time that another one signs in. – AL. Apr 27 '18 at 17:27