2

I'm having difficulty in passing background data to a screen. In the background the app is calling the screen correctly but the data that this screen needs that is in the "data" (id from data object) in the notification is not being picked up. In foreground I got "data" correctly.

notification json

{
    "to" : "akshih890uhnkjh389jfkn3...",
    "priority" : "normal",
    "time_to_live" : 0,
    "data" : {
        "type" : "my_type",
        "id" : "my_id"
    },
    "notification" : {
        "body" : "Test body",
        "title" : "Test title",
        "click_action" : ".MyActivity"
    }
}


 public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            String clickAction = remoteMessage.getNotification().getClickAction();

                Map<String, String> data = remoteMessage.getData();
                Gson gson = new Gson();
                MyObject myObject = new MyObject(data.get("id"), data.get("type"), remoteMessage.getNotification().getTitle(),
                    remoteMessage.getNotification().getBody());

                Intent intent = new Intent(myObject.getClickAction());
                intent.putExtra("id", myObject.getId());

                PendingIntent contentIntent = PendingIntent.getActivity(
                        this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                sendFCMNotification(title, body, contentIntent);

        }

MyActivity

 if(getIntent() != null) {

            Bundle extras = getIntent().getExtras();
            code = extras.getString("id");

        }
user6505882
  • 247
  • 3
  • 16
  • hi .. i think cause you have to pass also in additiotion to notification also message (notification is forforeground message for background) – federico scamuzzi Aug 02 '17 at 12:07
  • Possible duplicate of [Push notification works incorrectly when app is on background or not running](https://stackoverflow.com/questions/37876257/push-notification-works-incorrectly-when-app-is-on-background-or-not-running) – Tim Aug 02 '17 at 12:39

4 Answers4

1
int id = Integer.valueOf(remoteMessage.getData().get("id"));

Since you use remoteMessage.getData(), you get entire data object, not an object who has data json inside, you get actual data json with your fields.

String data = remoteMessage.getData();
Gson gson = new Gson();
MyObject myObject = gson.fromJson(data, MyObject.class);
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • Okay, well observed! But I still do not understand how to get the id from data object. It works only in foreground. The activity that is called as a result of the click_action is already expecting an id but comes null in the background. – user6505882 Aug 02 '17 at 12:56
  • @user6505882 You create a custom notification, with your own intent. Don't need to create custom actions. You define your own actions, and the firebase will do his own handling, not yours. You need to remove `"click_action" : ".MyActivity"` – Cătălin Florescu Aug 02 '17 at 13:01
  • If I remove the click_action, in the background the app does not open the corresponding screen only in foreground. In case this data object model was adopted in conjunction with iOS for the app to function properly. Already the object mentioned above is what I got closer to working on Android testing in postman. But in background I can not get the id on data. – user6505882 Aug 02 '17 at 13:20
  • In this case, i don't know. Make a convention with iOS app. If you add a click_action, android will handle it, not your custom intent. – Cătălin Florescu Aug 02 '17 at 13:28
1

If your app is in background, Firebase will not trigger onMessageReceived(). Why.....? I have no idea. In this situation, I do not see any point in implementing FirebaseMessagingService.

According to docs, if you want to process background message arrival, you have to send 'click_action' with your message. But it is not possible if you send message from Firebase console, only via Firebase API. It means you will have to build your own "console" in order to enable marketing people to use it. So, this makes Firebase console also quite useless!

There is really good, promising, idea behind this new tool, but executed badly.

I suppose we will have to wait for new versions and improvements/fixes!

Miky Braun
  • 21
  • 6
  • *Why.....? I have no idea.* it's because there is a notification key in the payload – Tim Aug 02 '17 at 12:39
  • Can Firebase data payload wake the app if background, or when the phone first starts, send the data to the app and in the app dispatch a local notification with buttons, because I'm having a hard time figuring this out! – Miguel Oct 17 '22 at 17:44
0

in the background, you can get data from intent extras if you want to call specific activity then specify it in click_action attribute of data

refer:

Firebase Server reference

Firebase Push

Hardik Mehta
  • 867
  • 1
  • 12
  • 20
0

I found the problem, when the server sends me the notification object in the background I can not see the date. But when the server sends only the date I get the id however how will I be able to build a notification without the title and body data?

user6505882
  • 247
  • 3
  • 16