收到推送通知时如何自动打开应用程序? [英] How to automatically open app when receive push notification?

查看:139
本文介绍了收到推送通知时如何自动打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在收到推送通知时自动打开应用程序. 我已经尝试过,但是仍然无法按预期工作. 下面的代码在应用处于活动状态或MainActivity时有效,但在后台运行或仅在托盘上显示通知时不起作用. 我错过了什么吗?

I want to automatically open app when receive push notification. I've tried but it still does not work as I expected. This code below is work when the app is active or in MainActivity, but it's not work when the app in the background or just show notification on tray. Did I miss something?

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification() != null) {
        if (PreferencesUtil.getInstance(this).isLoggedIn()) {
            sendNotification(remoteMessage.getData().get("order_id"));
        }
    }

}


public void sendNotification(String messageBody) {
    NotificationManager notificationManager = null;
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder notificationBuilder;

    notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Notification")
            .setSmallIcon(R.mipmap.icon_notif)
            .setContentText(messageBody)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_LIGHTS );
    //add sound
    try {
        Uri sound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.siren);
        Ringtone ringtone = RingtoneManager.getRingtone(this, sound);
        ringtone.play();
        notificationBuilder.setSound(sound);
    } catch (Exception e) {
        e.printStackTrace();
    }
    //vibrate
    long[] v = {1000, 1000, 1000, 1000, 1000};
    notificationBuilder.setVibrate(v);
    notificationManager.notify(0, notificationBuilder.build());

    Intent i = new Intent(this, NotificationActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}
}

推荐答案

这是需要从后端处理的东西,

This is something need to handle from backend,

这是您当前正在使用的示例有效负载, { "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Portugal vs. Denmark", "body":"great match!" } } }

Here is a sample payload you are using right now, { "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "notification":{ "title":"Portugal vs. Denmark", "body":"great match!" } } }

只有当您的应用程序处于前台时,这才可以让您控制操作并执行一些操作,否则只会发出通知.

Which will only give you control to manipulate and do some action when your app will be in foreground otherwise just raise notification.

详细信息,您可以在此处.

现在,要始终控制您的通知,您需要像下面这样的有效负载,

Now, To always get control over your notification, you need payload like following,

{ "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data":{ "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" } } }

{ "message":{ "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data":{ "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" } } }

区别在于您需要发送数据有效负载,而不是从后端发送通知poayload.

The difference is you need to send data payload instead of notification poayload from backend.

这篇关于收到推送通知时如何自动打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆