2

everybody, I m using firebase to receive a push notification from the server. Everything working fine when the application is running. I got the notification, I handle it and show it over notification tray. Seems perfect. Here is my code.

public class FirebasePushService extends FirebaseMessagingService {
    private static final String TAG = "FireBase main service ";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "Got Message: " + remoteMessage.getFrom());
        try {
            if (remoteMessage != null && remoteMessage.getNotification() != null
                    && remoteMessage.getNotification().getBody() != null) {
                String body = remoteMessage.getNotification().getBody();

                Log.d(TAG, "From: " + remoteMessage.getFrom());
                Log.d(TAG, "Notification Message Body: " + body );

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }}

Manifest code.

 <service android:name="app.asparagus.com.asparagus.firebase.FirebasePushService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

. The issue is when the application is closed. No log works of this class, nothing. But here is the interesting part. I can see the whole JSON from the server, and it is shown on notification tray ( whole JSON object is displayed). Really not getting what's wrong in my code. Check the image. 1- The success case. enter image description here

2- Unknown issue caseenter image description here

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Nouman Ghaffar
  • 3,780
  • 1
  • 29
  • 37

2 Answers2

0

From this link, I think the message should contain both notification and data payload.

Or maybe you can set the priority of the message to high like this one

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "priority" : "high",
  "notification" : {
    "body" : "This week's edition is now available.",
    "title" : "NewsMagazine.com",
    "icon" : "new"
  },
  "data" : {
    "volume" : "3.21.15",
    "contents" : "http://www.news-magazine.com/world-week/21659772"
  }
}
Faruk
  • 5,438
  • 3
  • 30
  • 46
0

Sorry for late reply I have found the solution and In case anyone else have issue with it.Here is from firebase documentation. Message types

With FCM, you can send two types of messages to clients:

Notification messages, sometimes thought of as "display messages." Data messages, which are handled by the client app. Notification messages contain a predefined set of user-visible keys. Data messages, by contrast, contain only custom key-value pairs. Notification messages can contain an optional data payload that is delivered when users tap on the notification.

Use scenario
Notification message FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

How to Send In a trusted environment such as Cloud Functions or your app server, use the Admin SDK or the HTTP and XMPP APIs: Set the notification key. May have optional data payload. Always collapsible. Use the Notifications composer: Enter the Message Text, Title, etc., and send. Add optional data payload by providing Custom data. Always collapsible.

Use scenario

Data message Client app is responsible for processing data messages. Data messages have only custom key-value pairs. In a trusted environment such as Cloud Functions or your app server, use the Admin SDK or the HTTP and XMPP APIs: Set the data key only. Can be either collapsible or non-collapsible.

Link

Nouman Ghaffar
  • 3,780
  • 1
  • 29
  • 37