[英]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. 根据文档 ,通知到达时有两种可能性。
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. 您已完成相关提及的此代码。
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. 我做到了这一点,它的作用对我而言。
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.
在后台,应用程序在通知托盘中接收通知有效负载,并仅在用户点击通知时处理数据有效负载。
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.