In the Android application, I have a service that listens to alerts.

@Override public void onMessageReceived(String from, Bundle data) { Log.e(TAG, "New message: " + data); String message = data.getString("message"); Log.d(TAG, "From: " + from); Log.d(TAG, "Message: " + message); sendNotification(message); } 

the problem is that this method can only listen to messages of this type.

 var message = new gcm.Message({ collapseKey: 'data', priority: 'high', contentAvailable: true, delayWhileIdle: false, timeToLive: 10000, data: { message: 'Message from gcm server', } }); 

Those. I can catch this message in the application and issue the necessary notification myself.

But if I send a message in this format

 var message = new gcm.Message({ collapseKey: 'data', priority: 'high', contentAvailable: true, delayWhileIdle: false, timeToLive: 10000, notification: { title: "Hello, World", icon: "ic_launcher", body: "This is a notification that will be displayed ASAP." } }); 

In the application, the onMessageReceived() method generally turns out that it does not work.

The documentation says that I can send hybrid messages that I understand should be processed by the onMessageReceived() method

Hybrid messages with both notification and data payload

This is the case when it comes to the background, or the foreground — whether it is active or not, it is active at the time of receipt.

If you’re in the background, you’ll receive your data payload. When in the foreground, your payloads are available. Here is a JSON-formatted message that can contain both notification and data:

{"to": "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx ...", "notification": {"body": "great match!", "title": "Portugal vs. Denmark", "icon": "myicon"}, " data ": {" Nick ":" Mario "," Room ":" PortugalVSDenmark "}}

The question is how can this be, and how can I catch messages of the latter type?

  • Why do you need to process it? This is an easy message, which immediately gets into the notification without any additional code, such as a feature. Add to it click_action (see the docks for details) and that's it. Send the rest and process via data ... - Yura Ivanov
  • In principle, you are right, here is the click_action account - where can you read about it because the dock says that you can click on it and perform the necessary action, but how can I do it? - Kirill Stoianov
  • developers.google.com/cloud-messaging/downstream#sample-receive "click_action": "OPEN_ACTIVITY_1" and in intent-filter <action android: name = "OPEN_ACTIVITY_1" /> - Yura Ivanov

1 answer 1

In general, there are two states push-notification

  • when the application is open

then push is processed in the onMessageReceived() method and there as you distribute all fields from the notification you make your notification and show, and in the body ( data ) you put it in the intent and by clicking on the notification open the activity with the necessary intent (At this point you need work out the json view so that there are no problems with the data).

  • the second is when the application is closed

then the application does not know that the push came, but the system knows about it, and then the notification field is notification the system and a push-уведомление , in the click_action field click_action can specify which activity to open ( TAG ), and in your application you can register this activity with TAG , then the system itself opens it and the intent formed by the system from the body ( data ) will immediately lie in it

And the rest of the work with intent will be on your shoulders.

And advice from yourself, go to FirebaseCloudMessage, a great wrapper over gcm.

  • Thanks, I have already figured out notifications, I already use Firebase! - Kirill Stoianov