FCM notifications and collapse_key

I send notifications to users of Android devices, although the Firebase Notification console, and I notice that even if I send 10 different notifications when the user device is disconnected, after the user goes online, he / she will receive all 10.

However, the Firebase documentation states that:

FCM allows you to use a maximum of four different camber keys for each device at any given time. In other words, the FCM connection server can simultaneously store four different resettable send messages for synchronization to the device, each with a different key to collapse. If you exceed this number, FCM saves only four collapse keys, without any guarantee as to which ones are saved.

So the user should not receive only 4 notifications? Am I missing something? (I do not extend FirebaseMessagingService , I leave notification processing in SDK)

UPDATE If you do not specify a failure key in the Firebase notification console, it seems that an implicit collapse key has been assigned to this notification, and this is the package name applications. I checked this by checking all key / value pairs of the key set getIntent().getExtras() as soon as I started the application by clicking on the notification. And indeed, I get the collapse_key with the value of the package name, even if I did not specify it.

UPDATE 2 . I tried to handle notifications by extending the FirebaseMessagingService to receive messages from the notification console when the application is in the foreground. I get a notification and I manually show the notification to the user. And guess what. Collapse keys work great! I get one notification, even if I send several notifications with the same collapse key. BUT this happens, obviously, only when the application is in the foreground, because the Firebase SDK does not call onMessageReceived() when the application is in the background, but instead it processes the notification itself. Does this mean that this is a Firebase SDK error? (since the problem only occurs when the notification displays the SDK)

So the question remains, why do I get all 10 notifications, since each notification has the same collapse key? Maybe an FCM error?

+3
source share
1 answer

After reading the post and comments, I don’t quite understand everything that was tried, which efforts were successful and which failed. I will cover the number if items and hope something useful.

Your message indicates that for some tests you specified a collapse key when you composed the message in the Firebase console. It's impossible. If you open the "Advanced" option and enter the key / value pair in the "User Data" section, this will not work. These values ​​are stored in the message under the data key, and not at the top level of the message in which collapse_key should appear. In addition, Table 1 in the documentation contains a warning that the data keys should not be reserved words in the table, in particular, referring to collapse_key :

The key must not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any words defined in this table (e.g. collapse_key ).

As noted in the comments to the message, the console automatically assigns a collapse key, which is the name of the package, so user input of the collapse key is not required.

However, my experience with the console matches yours. I create messages by entering only the message text and the device token. I do not see the processing of collapse; each message is received by the device. Based on my experience described below, this seems to be a problem with the console, and not with collapse handling in general. This is strange because if I send messages when the application is in the foreground and onMessageReceived() is onMessageReceived() , I have a debug log that displays the failure key in the message using getCollapseKey () . This conclusion confirms that the key is present and is the name of my application.

You will indicate that you have completed some tests sending notifications from the cloud function. I did my own testing using this cloud function and noticed that the expected message crash:

 exports.test = functions.database.ref('/test').onWrite(event => { const token = 'dK1FjGbNr6k:APA91bH7Vz3x...icGO56sJ7rAqOXRI'; console.log('Sending notification...'); const payload = { notification: { title: 'Message', body: 'Just one please!' } }; const options = { collapseKey: 'green' }; return admin.messaging().sendToDevice(token, payload, options).then(response => { console.log('Done'); }); }); 

I also sent this message using the Advanced Rest Client application browser, and also saw the message merge correctly:

 { "to": "dK1FjGbNr6k:APA91bH7Vz3x...O56sJ7rAqOXRI", "collapse_key": "green", "notification": { "title": "Message", "body": "Just one please!" } } 

I will also tell you that Firebase sends an analytics log message when it receives a notification. This is useful for testing when you want to get the number of messages received:

 D/FA: Logging event (FE): notification_receive(_nr), Bundle[{firebase_event_origin(_o)=fcm, message_device_time(_ndt)=0, message_time(_nmt)=1498227476, message_id(_nmid)=6447126672094369335}] 
+2
source

Source: https://habr.com/ru/post/1257500/


All Articles