1

Now I know that if I send data messages, the notifications will come even if the app is in the background. But I am receiving no notifications whatsoever even after sending data messages.

I'm using Postman for testing purposes and here's my body of my Postman Request:

{
 "to" : (device token),

 "data" : {
     "body" : "great match!",
     "title" : "Portugal vs. Denmark",
     "content_available" : true,
     "priority" : "high",
     "sound": "default",
     "time_to_live":"2419200"
  } 
}

And this is my onMessageReceived() funtion:

public void onMessageReceived(RemoteMessage remoteMessage) {

    Map<String,String> data=remoteMessage.getData();
    String title=data.get("title");
    String body=data.get("body");

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            sendPushNotification(title,body);

        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }
}

And this is what my sendPushNotification() function looks like:

private void sendPushNotification(String title,String message) {

    try {

        String imageUrl="";
        MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        if(imageUrl.equals("null")){
            mNotificationManager.showSmallNotification(title, message, intent);
            mNotificationManager.playNotificationSound();
        }else{
            mNotificationManager.showBigNotification(title, message, imageUrl, intent);
            mNotificationManager.playNotificationSound();
        }

    } catch (Exception e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    }
}

I was following this question on stack overflow : How to handle notification when app in background in Firebase

P.S.: When app is in the foreground, notifications are being received just fine.

KENdi
  • 7,576
  • 2
  • 16
  • 31
meagler
  • 287
  • 1
  • 2
  • 11
  • check this https://stackoverflow.com/questions/42932231/handling-data-payload-when-app-is-in-the-background – Peter Haddad Dec 27 '17 at 10:50
  • Thanks for the reply! But I'm using only a data payload..the question demands using notification payload too. Besides, I'm not able to see the notification anywhere. – meagler Dec 27 '17 at 11:00
  • okay to clarify you want both `data` and `notifcation`? or only one ? – Peter Haddad Dec 27 '17 at 11:08
  • I want a notification when the app is in the background...I'm actually really confused...I saw the answers for other similar questions where they said that I need to use a data payload and only then will the notification be received when the app is in the background..can you help? – meagler Dec 27 '17 at 11:13

4 Answers4

2

As you could see on my post, for specific ids you should use "registration_ids": ["{device-token}","{device2-token}","{device3-token}"] key and values instead of ”to”. That’s for topics

Antonio
  • 11,413
  • 6
  • 34
  • 48
  • Hey! Thanks for taking a look and the answer! I tried what you've suggested but the problem is, I get an error saying "InvalidRegistration" even though the device token is correct – meagler Dec 27 '17 at 13:55
  • Oh, I forgot to ask: what devices are you using? That’s important – Antonio Dec 27 '17 at 14:13
  • I'm using a Lenovo Vibe K5 – meagler Dec 28 '17 at 13:21
  • what do you think I should do? – meagler Jan 06 '18 at 13:54
  • Hey! Well, I would suggest finding a project that you certainly know that is working (search for a google sample or something), try it on your phone. If this doesn’t work, you can post your issue on the google forums or support – Antonio Jan 06 '18 at 15:41
  • 1
    Hey man! Thank you so much for all the help! The notifications are working perfectly in Moto, Redmi, Samsung and most other devices except mine which is a Lenovo Vibe K5 and other Android Oreo devices which is quite manageable. A big thanks again! You really helped a lot! :D – meagler Jan 23 '18 at 15:42
  • 1
    Hey! I'm glad I could help. I wish you can solve the issue for you phone in the future. It should be an issue with FCM or Lenovo implementation. Let me know if I can help in any other way. Happy code! – Antonio Jan 23 '18 at 23:15
1

You need to define click_action in your notification payload.

"notification"{
     "click_action":"YOUR_ACTIVITY_NAME"
  }

also in your manifest edit the activity definition as below

<activity
            android:name="com.demo.xyzActivity"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name=".xyzActivity" /> //This should be one set into payload
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

This will open your activity when app will be in background.

karan
  • 8,637
  • 3
  • 41
  • 78
0

are you using notification payload along with data payload? as is the function finds notification payload it sends it directly to notification tray and hence data payload is neglected. change

JSONObject json = new JSONObject(remoteMessage.getData().toString());

to

Map<String, String> bundleData = remoteMessage.getData();

this will remove try catch and will let you know in its causing exception.

0

Create the BroadcastReceiver

Declare in Manifest.xml

    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <receiver
                android:name=".OnBootBroadcastReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
    </receiver>

Create OnBootBroadcastReceiver.java file and call the FCM service in it.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class OnBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
        i.setClass(context, FirebaseMessagingReceiveService.class);
        context.startService(i);

    }
}
jessica
  • 1,700
  • 1
  • 11
  • 17
  • What is `FirebaseMessagingReceiveService.class` ? – meagler Jan 02 '18 at 12:23
  • FirebaseMessagingReceiveService is the class which is called when receiving any message from FCM, its name may be different in your case. For example, the class which extends FirebaseMessagingService and having onMessageReceived() method. – jessica Jan 03 '18 at 05:58