Android Firebase Push notification not working whe

2020-08-01 05:02发布

问题:

I have created android application to receive push notification from server but it is not working.

When app is in foreground, it works perfectly. But when we force close app from system tray it does not work.

Below is my json code which I send to FCM.

{"to":"\/topics\/global","data":{"title":"Title","body":"Body"}}

Below is my android function

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

        Log.d("Msg", "Message received ["+remoteMessage+"]");

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


        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        db.addData(data.get("body"));

        Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
        resultIntent.putExtra("message", data.get("body"));

        showNotificationMessage(getApplicationContext(), data.get("title"), data.get("body"), "", resultIntent);
    }

Below is code to show notification.

private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
        notificationUtils = new NotificationUtils(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
    }

I can not find any problem, can any one please help me.

Thank you very much in advance.

Edit :

I have solved my problem by changing phone, in samsung device code is working fine but its problem with MI device itself.

I have also got solution for MI devices, use below steps to enable auto start that application.

Go to settings --> permissions --> autostart. From there, pick the apps you want to receive notifications, and toggle the switch to turn it on.

回答1:

As per the doc there are two possibilities when notification arrived.

  1. Application is in foreground.

You will get notify in onMessageReceived, and you can get data in this method. You need to generate local notification in system tray.

Handle Notification

You have done this code as mentioned in question.

  1. Application is in background.

You get notification in system tray. You don't need to generate notification here. You will get data in Intent of launcher Activity.

Handle Notification

Here is code of launcher activity

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent == null || intent.getExtras() == null) {
            // your code to handle if there is no notification
        } else {
            // handle notification
            String body = intent.getStringExtra("body");
            String title = intent.getStringExtra("title");
            //TODO your code to open activity as per notification
        }
    }

I have done this, and its works for me.



回答2:

From here,

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

And from this,

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity

You might get complete help from receive message tutorial