如何在收到推送通知时自动打开应用程序?

时间:2018-05-16 10:40:52

标签: android firebase push-notification firebase-cloud-messaging

我希望在收到推送通知时自动打开应用。 我已经尝试过,但它仍然无法正常工作。 以下代码适用于应用处于活动状态或MainActivity状态,但当应用在后台或仅在托盘上显示通知时,它无效。 我错过了什么吗?

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);
}
}

3 个答案:

答案 0 :(得分:1)

这是需要从后端处理的事情,

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

只有当您的应用处于前台时,才能让您控制操作并执行某些操作,否则只会提出通知。

详细信息,您可以查看here

现在,要始终控制通知,您需要有效负载,如下所示

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

不同之处在于您需要从后端发送数据有效负载而不是通知poayload。

答案 1 :(得分:0)

int requestID = (int) System.currentTimeMillis();
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

并像这样添加PendingIntent

notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle("Notification")
            .setSmallIcon(R.mipmap.icon_notif)
            .setContentText(messageBody)
            .setContentIntent(contentIntent);
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_LIGHTS );

答案 2 :(得分:0)

首先,Android中“应用程序”的概念略有扩展。

应用程序 - 从技术上讲是一个过程 - 可以有多个活动,服务,内容提供者和/或广播听众。如果其中至少有一个正在运行,则应用程序已启动并正在运行(该过程)。

所以,你需要确定的是你想如何“启动应用程序”。

好的......这是你可以试试的:

  1. 使用category=LAUNCHERPackageManager
  2. 创建一个意图
  3. 使用context.getPackageManager
  4. 从当前上下文中获取packageManager.queryIntentActivity(<intent>, 0)
  5. category=LAUNCHER, action=MAIN其中intent有packageManager.resolveActivity(<intent>, 0)ActivityInfo来获取main / launcher的第一个活动
  6. 获取您感兴趣的ActivityInfo
  7. packageName获取category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name)和姓名
  8. 最后,使用context.startActivity(newIntent)和setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)创建另一个意图
  9. 最后,{{1}}
相关问题