android 杀死程序收不到推送,应用程序被杀死时,Android Firebase推送通知不起作用...

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 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.

解决方案

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

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值