3

I am Creating an App using Firebase cloud messaging API... I am able to send notification and data to my client application from the server. But the problem is When the application is open the notification is not firing while the data appears (I Mean I logged it) It is not an issue. But when the application is closed, the notification is received, while I clicked on notification the activity is opened while I am not able to see the log data. I need to Update the data to a TextView..

My MyFirebaseMessagingService :

public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        Log.e("FROM", "From: " + remoteMessage.getFrom());
        String data = remoteMessage.getData().get("message");
        Log.e("KOIII", "Notification Message Body: " + remoteMessage.getNotification().getBody());
       // showNotificationS(remoteMessage.getNotification().getBody(),2);
        if(data.equals("true")){
            Log.e("DATA", "SUCCESS");
           // Fragment1.getInstace().updateTheTextView(data, data, data);
        }
    }

My manifest:

<activity
            android:name=".SplashScreen"
            android:label="@string/app_name"
            android:launchMode="singleInstance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".myHome"
            android:label="@string/app_name">
        </activity>
<service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".FirebaseIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
Karthik CP
  • 1,150
  • 13
  • 24

2 Answers2

8

When you send a notification message with a data payload (notification and data) and the app is in the background you can retrieve the data from the extras of the intent that is launched as a result of the user tapping on the notification.

From the FCM sample which launches the MainActivity when the notification is tapped:

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        Object value = getIntent().getExtras().get(key);
        Log.d(TAG, "Key: " + key + " Value: " + value);
    }
}
476rick
  • 2,764
  • 4
  • 29
  • 49
Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • 1
    does this get the data value?? or notification value to activity? – Karthik CP Aug 11 '16 at 16:36
  • 2
    The extras would be the key/value pairs from the data payload only. – Arthur Thompson Aug 11 '16 at 16:43
  • @ Arthur Thompson I tried and add that to my Activity.. But No log is seen.. Should i have to put that in fragment? Because as i am using fragments.. – Karthik CP Aug 12 '16 at 06:25
  • Does the intent have any extras that are logged at all when you tap on the notification? Also the request must contain a data payload, maybe add your request (if you are not using the console) and your code to log the data payload values to the question. – Arthur Thompson Aug 12 '16 at 12:41
4

FCM will not show notification if your app is in foreground, So you have manually show that like this:-

@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "From: " + remoteMessage.getData().get("message"));
//        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getData().get("message"));
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MarketActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Fred Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19
  • yeah i am able to do that.. but what i need is that.. if app is in background and the notification is fired.. but no log is seen in my logcat as i am printing the data.. i hope you get the scenario.. – Karthik CP Aug 11 '16 at 06:59
  • 1
    @KarthikCP Refer here http://stackoverflow.com/questions/38867199/how-get-message-body-when-fcm-notification-message-is-tap/ – Jagjit Singh Aug 11 '16 at 07:12
  • Yeah.. I got it.. The data and notification if both is used this problem exist i hope.. If only data is used then message will be recieved all times and can create Notification from that.. http://stackoverflow.com/a/37876727/3910281 – Karthik CP Aug 11 '16 at 08:24
  • can you please explain what the number 0 does in the code above? I have tried this but it won't show up the notification although I can see the message being received in the log. – Syed Ali Dec 24 '22 at 19:02