推送通知始终打开MainActiviy

时间:2019-12-08 16:16:32

标签: android push-notification

我创建了一个云功能,该功能可以随通知一起发送数据。现在,我想始终打开特定的活动。不幸的是,它总是开放MainActivity。这是我的Firebase服务代码:

   @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> dataSet = remoteMessage.getData();
    if (!dataSet.get("kind").equals("0")) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.baseline_send_24)
                        .setContentTitle(remoteMessage.getData().get("title"))
                        .setContentText(remoteMessage.getData().get("body"))
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.


        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }else{
        Intent intent = new Intent(this, SettingsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.baseline_send_24)
                        .setContentTitle(remoteMessage.getData().get("title"))
                        .setContentText(remoteMessage.getData().get("body"))
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.


        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }


}

我不知道是否必须在其他地方定义它。谢谢

2 个答案:

答案 0 :(得分:0)

 Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction("ACTION.HOME_ACTION");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

并在通知中设置此待定意图

.setContentIntent(pendingIntent)

答案 1 :(得分:0)

如果您将Firebase用于推送通知。如果您使用“通知”类型的有效负载,则在从服务器发送消息时,Firebase会管理应用程序的通知显示,而不会提供回调,并且默认情况下,它将始终打开应用程序的启动器活动。

您可以从服务器端发送额外的数据,在启动器活动getIntent方法中访问该数据,并根据需要重定向到其他活动