简体   繁体   English

应用程序被杀时,Android Firebase Push通知无效

[英]Android Firebase Push notification not working when application is killed

I have created android application to receive push notification from server but it is not working. 我已经创建了android应用程序来接收来自服务器的推送通知,但它无法正常工作。

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. 下面是我发给FCM的json代码。

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

Below is my android function 下面是我的android功能

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. 我通过更换手机解决了我的问题,在三星设备代码工作正常,但它与MI设备本身的问题。

I have also got solution for MI devices, use below steps to enable auto start that application. 我也有MI设备的解决方案,使用以下步骤启用该应用程序的自动启动。

Go to settings --> permissions --> autostart. 转到设置 - >权限 - >自动启动。 From there, pick the apps you want to receive notifications, and toggle the switch to turn it on. 从那里,选择要接收通知的应用,并切换开关以将其打开。

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. 您将在onMessageReceived获得通知,并且您可以使用此方法获取数据。 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. 您将在启动器活动的Intent中获取数据。

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. 我做到了这一点,它的作用对我而言。

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). 这包括包含通知和数据有效负载的消息(以及从Notifications控制台发送的所有消息)。 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 您可以从接收消息教程中获得完整的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序被杀死时,FireBase仅在以下Android 6.0设备中不接收推送通知 - FireBase not receiving push notification only in below android 6.0 device when Application killed firebase 推送通知在某些设备 (Android) 中被杀死的应用程序不起作用 state - firebase push notification is not working on app killed state in some devices (Android) 当从联想等某些设备上的多任务托盘中杀死应用程序时,Android应用程序未收到Firebase推送通知 - Android app not receiving Firebase Push Notification when application is killed from multi-task tray on some devices like lenovo 如果应用程序被杀死,则解析推送通知不起作用(Android) - Parse Push notification not working if app is killed (Android) Android-Firebase推送通知不起作用 - Android - Firebase push notification not working Android中的Firebase推送通知不起作用 - Firebase push notification in android not working 当应用程序处于后台或被杀死时,我想在 Android 中显示自定义 firebase 推送通知 state - I want to show a customize firebase push notification in Android when app is in background or killed state android - Firebase 通知推送通知不起作用 - android - Firebase Notification Push Notification not working 当应用程序被杀死或在Mi 4i中强制停止时,android推送通知不起作用 - android push notification is not working when app Killed or force stop in Mi 4i 当 android 应用程序被杀死时,react-native-push-notification 自定义声音不起作用 - react-native-push-notification custom sound not working when android app is killed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM