I’m learning Android not so long ago, now there is a need to make push notifications. Put firebase, registered the onMessageReceived method, but as far as I understood, it does not work in the background. How to make it so that when an application in the background displays a push with a custom icon?
- If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
1 answer
Yes, onMessageReceived come only to the onMessageReceived method when the application is active. When the application is in the background - the message immediately falls into the notification. You can set the click_action property on this message and in your application catch it via intent-filter .
Example
The body of the message that the server sends:
var message = new gcm.Message({ collapseKey: 'data', priority: 'high', contentAvailable: true, delayWhileIdle: false, timeToLive: 10000, data: { message: 'Message from gcm server', action: 'some action' }, notification: { tag : 'hasData', title: "English for Founders", icon: "ic_launcher", color: "#22C064", sound: "notification_sound", body: "This is a GCM notification that will be displayed ASAP.", click_action: "OPEN_APP" // make intent-filter in Manifest.xml for this action } }); You have a filter on action - OPEN_APP for a certain OPEN_APP registered in Manifest.xml .
As a result, the phone will receive a push-уведомление from which you can get click_action and when the Activity starts, in the onCreate method you can also get data from this message, like this:
public static void parseExtras (Bundle intentExtras){ if (intentExtras != null) { for (String key : intentExtras.keySet()) { try { String value = intentExtras.getString(key); Log.wtf(TAG, "Key: " + key + " Value: " + value); }catch (ClassCastException e){ Log.wtf(TAG, "Cannot parse extras, ClassCastException");} } } } And if you need to display a picture, write its name in the icon tag
- Thank! And tell me more, is it possible for the application to process the message? That is, onMessageReceived even to work in the background? For example, that in some city the notification comes, and in the other there isn’t? - Bullshit
- As far as I understand, this is not possible - Kirill Stoianov