firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.87k stars 7.32k forks source link

I receive only last one message when turn on internet? #1007

Open Xing1P opened 4 years ago

Xing1P commented 4 years ago

send with notification payload for background notification. but if send without "notification" payload i receive notification is normal when send while device off internet and then device turn on internet. https://fcm.googleapis.com/fcm/send

{
    "to":"....",
    "data":{
        "destination":"1",
        "source":"2",
        "badge":"1"},
    "notification":{
            "title":"Hello7","body":"Hello Test"
        },
    "priority":"high"
}

Step 1: Describe your environment

Step 2: Describe the problem:

Steps to reproduce:

  1. Turn off internet
  2. send fcm message 10
  3. Turn on internet
  4. Receive only last one message and 9 message not receive Note**: but when send notification while device online notification receive normal

Observed Results:

2019-12-03 14:03:47.765 4931-4931/? V/FA-SVC: NetworkBroadcastReceiver received action: android.net.conn.CONNECTIVITY_CHANGE 2019-12-03 14:03:47.801 4931-15302/? V/FA-SVC: Unregistering connectivity change receiver 2019-12-03 14:03:47.802 4931-15302/? V/FA-SVC: Upload scheduled in approximately ms: 3047916 2019-12-03 14:03:47.802 4931-15302/? V/FA-SVC: Cancelling job. JobID: 812057698 2019-12-03 14:03:47.811 4931-15302/? V/FA-SVC: Scheduling upload with GcmTaskService 2019-12-03 14:03:47.811 4931-15302/? V/FA-SVC: Scheduling task with Gcm. time: 3047916 2019-12-03 14:03:48.216 10173-15301/com.tss.testfcmraw D/FA: Logging event (FE): notification_receive(_nr), Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}] 2019-12-03 14:03:48.309 10173-15301/com.tss.testfcmraw V/FA: Connecting to remote service 2019-12-03 14:03:48.330 10173-15301/com.tss.testfcmraw D/FA: Connected to remote service 2019-12-03 14:03:48.333 10173-15301/com.tss.testfcmraw V/FA: Processing queued up service tasks: 1 2019-12-03 14:03:48.367 4931-15302/? V/FA-SVC: Logging event: origin=fcm,name=notification_receive(_nr),params=Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}] 2019-12-03 14:03:48.395 4931-15302/? V/FA-SVC: Saving event, name, data size: notification_receive(_nr), 35 2019-12-03 14:03:48.396 4931-15302/? V/FA-SVC: Event recorded: Event{appId='com.tss.testfcmraw', name='notification_receive(_nr)', params=Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}]} 2019-12-03 14:03:48.398 4931-15302/? V/FA-SVC: Upload scheduled in approximately ms: 3047320 2019-12-03 14:03:48.399 4931-15302/? V/FA-SVC: Cancelling job. JobID: 812057698 2019-12-03 14:03:48.402 4931-15302/? V/FA-SVC: Scheduling upload with GcmTaskService 2019-12-03 14:03:48.402 4931-15302/? V/FA-SVC: Scheduling task with Gcm. time: 3047320 2019-12-03 14:03:48.407 4931-15302/? V/FA-SVC: Background event processing time, ms: 41 2019-12-03 14:03:53.373 10173-15301/com.tss.testfcmraw V/FA: Inactivity, disconnecting from the service

Expected Results:

When turn on internet i want to get 10 notification.

Relevant Code:

  // TODO(you): code here to reproduce the problem
samtstern commented 4 years ago

@pahnpiseth this isn't enough information to tell what's going on here. Can you show more about how you're sending these messages? Is the behavior thee same if the device stays online the whole time?

Xing1P commented 4 years ago

i'm edited hope it enough info...please help.

kroikie commented 4 years ago

@pahnpiseth you mention

send with notification payload for background notification. but if send without "notification" payload i receive notification is normal when send while device off internet and then device turn on internet.

Your example payload has a notification object defined. So is the problem happening with or without the notification payload?

kroikie commented 4 years ago

@pahnpiseth Note that notification-messages messages with a notification payload object are collapsed on the server by default if no collapse_key is defined. You can manually define a collapse_key to keep these messages from collapsing on each other. However you are limited on the number of concurrent collapse keys that the FCM backend will maintain.

Does this resolve your issue?

Xing1P commented 4 years ago

okay,brother mean when send message fcm with "notification" payload cannot use with non-collapse key?

Xing1P commented 4 years ago

okay,brother mean when send message fcm with "notification" payload cannot use with non-collapse key?

FCM document not mention this.

macroday commented 4 years ago

I have checked with and without collapse key but only last message come when turn on internet. Please tell me how to fix this issue or give some suggestion to fix it.

Sammy826 commented 3 years ago

send with notification payload for background notification. but if send without "notification" payload i receive notification is normal when send while device off internet and then device turn on internet. https://fcm.googleapis.com/fcm/send

{
  "to":"....",
  "data":{
      "destination":"1",
      "source":"2",
      "badge":"1"},
  "notification":{
          "title":"Hello7","body":"Hello Test"
      },
  "priority":"high"
}

Step 1: Describe your environment

  • Android device: test on Pixel 2xl,Oneplus6,Oppo F7
  • Android OS version: Android 10,9,8
  • Google Play Services version: Last
  • Firebase/Play Services SDK version: Last

Step 2: Describe the problem:

Steps to reproduce:

  1. Turn off internet
  2. send fcm message 10
  3. Turn on internet
  4. Receive only last one message and 9 message not receive Note**: but when send notification while device online notification receive normal

Observed Results:

2019-12-03 14:03:47.765 4931-4931/? V/FA-SVC: NetworkBroadcastReceiver received action: android.net.conn.CONNECTIVITY_CHANGE 2019-12-03 14:03:47.801 4931-15302/? V/FA-SVC: Unregistering connectivity change receiver 2019-12-03 14:03:47.802 4931-15302/? V/FA-SVC: Upload scheduled in approximately ms: 3047916 2019-12-03 14:03:47.802 4931-15302/? V/FA-SVC: Cancelling job. JobID: 812057698 2019-12-03 14:03:47.811 4931-15302/? V/FA-SVC: Scheduling upload with GcmTaskService 2019-12-03 14:03:47.811 4931-15302/? V/FA-SVC: Scheduling task with Gcm. time: 3047916 2019-12-03 14:03:48.216 10173-15301/com.tss.testfcmraw D/FA: Logging event (FE): notification_receive(_nr), Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}] 2019-12-03 14:03:48.309 10173-15301/com.tss.testfcmraw V/FA: Connecting to remote service 2019-12-03 14:03:48.330 10173-15301/com.tss.testfcmraw D/FA: Connected to remote service 2019-12-03 14:03:48.333 10173-15301/com.tss.testfcmraw V/FA: Processing queued up service tasks: 1 2019-12-03 14:03:48.367 4931-15302/? V/FA-SVC: Logging event: origin=fcm,name=notification_receive(_nr),params=Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}] 2019-12-03 14:03:48.395 4931-15302/? V/FA-SVC: Saving event, name, data size: notification_receive(_nr), 35 2019-12-03 14:03:48.396 4931-15302/? V/FA-SVC: Event recorded: Event{appId='com.tss.testfcmraw', name='notification_receive(_nr)', params=Bundle[{ga_event_origin(_o)=fcm, message_type(_nmc)=display}]} 2019-12-03 14:03:48.398 4931-15302/? V/FA-SVC: Upload scheduled in approximately ms: 3047320 2019-12-03 14:03:48.399 4931-15302/? V/FA-SVC: Cancelling job. JobID: 812057698 2019-12-03 14:03:48.402 4931-15302/? V/FA-SVC: Scheduling upload with GcmTaskService 2019-12-03 14:03:48.402 4931-15302/? V/FA-SVC: Scheduling task with Gcm. time: 3047320 2019-12-03 14:03:48.407 4931-15302/? V/FA-SVC: Background event processing time, ms: 41 2019-12-03 14:03:53.373 10173-15301/com.tss.testfcmraw V/FA: Inactivity, disconnecting from the service

Expected Results:

When turn on internet i want to get 10 notification.

Relevant Code:

// TODO(you): code here to reproduce the problem
Sammy826 commented 3 years ago

@pahnpiseth this isn't enough information to tell what's going on here. Can you show more about how you're sending these messages? Is the behavior thee same if the device stays online the whole time?

ArthurKun21 commented 2 years ago

Hello,

is there any solution to this issue?

I have also been trying to this

i tested this with the tool from the FCM https://console.firebase.google.com/project/__/notification/compose

while it is successfully receives all notification when it is online, once the device turns offline. Sending multiple FCM notifications and once the network have return. Only the last FCM notification will be receive.

hoping to get some solutions

danielaxo commented 2 years ago

😭😭😭😭m

nnnn

Msalati commented 1 year ago

Has anyone solved this issue yet?

untrueme commented 1 year ago

Same problem

zell180 commented 1 year ago

i'm having the same issue

Msalati commented 1 year ago

The issue is still present unfortunately and I've had tested it in all possible scenarios.

I've read the Firebase documentation carefully to attempt and figure out a solution. nothing seems to fix this yet.

I've been using the Firebase legacy API and I've upgraded to Firebase Cloud Messaging API (V1) to verify whether or not the issue was caused by the legacy API, I can confirm it happens on both.

Steps to produce if anyone is still uncertain about how it happens: 1- Have a device FCM registered and has opened the app at least once. (tested on a physical android phone) 2- Turn off the internet on that device. 3- Attempt sending various messages to that device e.g. 5 Messages. 4- Turn on the internet

You will notice that only the last message out of the five messages will be delivered.

I've played around with the priorities just in case but no luck it keeps on happening.

zell180 commented 1 year ago

Hello everyone, I'm trying to clarify this problem which in reality is not a problem but depends on how Android and Firebase decide to manage notifications.

There are two types of notifications: 1) those that contain the key of the json notification and data 2) those that instead contain only the given key data without the notification key

As per Google documentation, notifications that have the notification key are considered "always collapsible" https://firebase.google.com/docs/cloud-messaging/concept-options

This means that when the notifications are sent, a collapse_key with this value com.organization.myapp is automatically added in the json of the notification

Even if you try to change this value by changing the "collapse_key" key to a random value that always change, the Firebase SDK will overwrite that value again with "com.organization.myapp" on sending the notification.

In this way, exactly the behavior we complain about is manifested: if we put the smartphone in airplane mode and send 5 notifications, we will only receive the last one. This is because the collapse_key is always the same and therefore FCM will overwrite the notifications with the same key keeping only the last one and delivering only that one to the user.

If, on the other hand, we send a notification without the notification key and only with the data key, this will have no "collapse_key" and in this way all notifications will be delivered without any problems.

Obviously notifications without the notification key are not delivered to the user like those that do contain it and therefore a personal way must be created to make the notification pop-up appear on the user's smartphone

untrueme commented 1 year ago

i've tried to implement data notifications only, the results are the same

zell180 commented 1 year ago

i've tried to implement data notifications only, the results are the same

impossible, i was stuck on that problem and i've solved it. you specifiy "collapse_key" in your json? can you post your notification json or configuration?

untrueme commented 1 year ago
await messaging.send({
        token: 'token',
        data: {
            'messageId': messageId.toString(),
        },
        android: {
            priority: "high",
            notification: {
                sound: "default",
                default_vibrate_timings: false,
                vibrate_timings: ['0.1s', '0.5s', '0.1s', '0.5s'],
            },
            "ttl": 240000
        }
zell180 commented 1 year ago
await messaging.send({
        token: 'token',
        data: {
            'messageId': messageId.toString(),
        },
        android: {
            priority: "high",
            notification: {
                sound: "default",
                default_vibrate_timings: false,
                vibrate_timings: ['0.1s', '0.5s', '0.1s', '0.5s'],
            },
            "ttl": 240000
        }

You are using "notification" key inside "android" key. is the same of use "notification" in root of message configuration. "android" key is specific for Android but in this way you're sending a notification+data noitification

untrueme commented 1 year ago

without notification key notifications doesn't appear

zell180 commented 1 year ago

without notification key notifications doesn't appear

And this is what i've write. if you want non collapsible notification you need to use external library to show notification pop-up, for example notifee, or write you're own way to show notification pop-up when you receive notification. This is the only way. Sounds crazy but on android notifications works in this way

untrueme commented 1 year ago

Data messages works good. Thanks!

atultiwaree commented 1 year ago

Please do leave code guys, If anyone solved the problem...I'm stuck

untrueme commented 1 year ago
await messaging.send({
                                    token: token,
                                    data: {
                                        'title': title,
                                        'body': body
                                    },
                                    android: {
                                        priority: "high",
                                        ttl: ttl
                                    }
                                });

hope it will help

atultiwaree commented 1 year ago
await messaging.send({
                                    token: token,
                                    data: {
                                        'title': title,
                                        'body': body
                                    },
                                    android: {
                                        priority: "high",
                                        ttl: ttl
                                    }
                                });

hope it will help

Thanks @untrueme I'll use notifee to handle the notification

magdyebrahim24 commented 1 year ago

Hello everyone, I'm trying to clarify this problem which in reality is not a problem but depends on how Android and Firebase decide to manage notifications.

There are two types of notifications:

  1. those that contain the key of the json notification and data
  2. those that instead contain only the given key data without the notification key

As per Google documentation, notifications that have the notification key are considered "always collapsible" https://firebase.google.com/docs/cloud-messaging/concept-options

This means that when the notifications are sent, a collapse_key with this value com.organization.myapp is automatically added in the json of the notification

Even if you try to change this value by changing the "collapse_key" key to a random value that always change, the Firebase SDK will overwrite that value again with "com.organization.myapp" on sending the notification.

In this way, exactly the behavior we complain about is manifested: if we put the smartphone in airplane mode and send 5 notifications, we will only receive the last one. This is because the collapse_key is always the same and therefore FCM will overwrite the notifications with the same key keeping only the last one and delivering only that one to the user.

If, on the other hand, we send a notification without the notification key and only with the data key, this will have no "collapse_key" and in this way all notifications will be delivered without any problems.

Obviously notifications without the notification key are not delivered to the user like those that do contain it and therefore a personal way must be created to make the notification pop-up appear on the user's smartphone

this soluation is worked for me also need to handle background notification beside

MiloudiMohamed commented 10 months ago

I've tested the solution suggested by @zell180 , it only works when the app is opened (open the app, turn off wifi, send notifications, turn on wifi). But when the app is closed, no notification is received, until I open it and then I receive again one notification

Anyone has some updates on this?

zell180 commented 10 months ago

I've tested the solution suggested by @zell180 , it only works when the app is opened (open the app, turn off wifi, send notifications, turn on wifi). But when the app is closed, no notification is received, until I open it and then I receive again one notification

Anyone has some updates on this?

Can you paste notification that you’re try to send? Android or iOS?

MiloudiMohamed commented 10 months ago

I'm using and testing on Android only

await messaging.send({
  token: 'abc123',
  data: {
      title: 'the title',
      body: 'the body',
  },
  android: {
      priority: "high",
      ttl: '2419200s'
  }
})

This as I said works when the app is open, but get ignored when it's not

atultiwaree commented 10 months ago

I'm using and testing on Android only

await messaging.send({
  token: 'abc123',
  data: {
      title: 'the title',
      body: 'the body',
  },
  android: {
      priority: "high",
      ttl: '2419200s'
  }
})

This as I said works when the app is open, but get ignored when it's not

After receiving last notification when turned on internet why don't you use socket to grap the notifications saved on server and show it as locally, because their is no solution for this issue till now.

MiloudiMohamed commented 10 months ago

I'm not saving the notifications on the server unfortunately. but I might refactor to use your suggestion.

Is this a Firebase issue, if so, is there any alternative that provides this functionality?

atultiwaree commented 10 months ago

I'm not saving the notifications on the server unfortunately. but I might refactor to use your suggestion.

Is this a Firebase issue, if so, is there any alternative that provides this functionality?

As far I did my research unfortunately found nothing... Rather than implementing in above way

Dasrath commented 2 months ago

Hi Did you find any solution for the above issue?

QL-Yugesh-Gupta commented 1 month ago

It is working now for Android. How to do same with APNs?