Android 特定活动未从通知中打开当应用程序不在前台时单击(应用程序处于最近状态,未终止!)

Android 特定活动未从通知中打开当应用程序不在前台时单击(应用程序处于最近状态,未终止!),android,firebase,push-notification,firebase-cloud-messaging,android-push-notification,Android,Firebase,Push Notification,Firebase Cloud Messaging,Android Push Notification,我想在通知单击时打开特定活动。但是当应用程序在后台时,它不会打开。我甚至没有传递额外的信息(即数据)。我只想打开活动,并根据用户登录,我做一些任务。我甚至尝试在通知单击时打开默认的启动程序活动,并从那里将用户发送到通知活动。以下是我的默认启动程序活动的代码: PS:我正在从firebase控制台发送消息,但它只有标题和正文 (这是我在完成网络任务后调用的函数:) if(list.size()!=0){ 对于(int i=0;i这仅在应用程序位于前台时有效。当应用程序位于后台时,它只考虑来自有效负

我想在通知单击时打开特定活动。但是当应用程序在后台时,它不会打开。我甚至没有传递额外的信息(即数据)。我只想打开活动,并根据用户登录,我做一些任务。我甚至尝试在通知单击时打开默认的启动程序活动,并从那里将用户发送到通知活动。以下是我的默认启动程序活动的代码: PS:我正在从firebase控制台发送消息,但它只有标题和正文

(这是我在完成网络任务后调用的函数:)

if(list.size()!=0){

对于(int i=0;i这仅在应用程序位于前台时有效。当应用程序位于后台时,它只考虑来自有效负载的通知数据并忽略数据部分。这导致无法控制应用程序,因为通知类型的消息将由系统单独处理。唯一的选项是使用您自己的外部服务器或从rest客户端发送。

您可以当应用程序处于前台或后台时,从Firebase通知重定向,直到您发送数据为止

我将为您提供一个示例,说明如何根据从通知中接收到的密钥在活动之间重定向,firebase神奇地处理其余部分

p.S:如果要处理已在后台打开的活动,请使用
taskafinity
进行处理

通过
FirebaseMessagingService
处理Firebase数据:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    System.out.println("MESSAGE BODY :" + remoteMessage.toString());
    if (remoteMessage.getData().size() > 0) {
        //getting the title and the body
        String redirect;
        String title = remoteMessage.getData().get("message_title");
        String body = remoteMessage.getData().get("message_body");
        redirect = remoteMessage.getData().get("redirect");
        String event_id = remoteMessage.getData().get("event_id");
        System.out.println("PUSH MESSAGE = " + remoteMessage.toString());
        JSONObject jsonData = new JSONObject(remoteMessage.getData());
        System.out.println("RESPONSE :" + jsonData.toString());
        if (redirect != null) {
            sendNotification(title, body, event_id, redirect);
        } else {
            redirect = "";
            sendNotification(title, body, event_id, redirect);

        }
    }
}


private void sendNotification(String title, String body, String event_id, String redirect) {
    Intent backIntent = new Intent();
    PendingIntent pendingIntent;
    if (redirect.contentEquals("CHAT")) {
        backIntent = new Intent(MyFirebaseMessagingService.this, ChatScreen.class);
        backIntent.putExtra("item_id", event_id);
    }
    if (redirect.contentEquals("EVENT") || redirect.contentEquals("INVITATION")) {
        backIntent = new Intent(MyFirebaseMessagingService.this, Event_Summary.class);
        backIntent.putExtra("event_id", event_id);
    }
    backIntent.putExtra("MODE", "FIRE_NOTIFICATION");
    backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntentWithParentStack(backIntent);
    pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

Firebase console仅发送通知消息(此处有更多信息),但您可以使用自己的服务器或Firebase API发送数据消息

数据消息适用于前台和后台应用程序状态。

触发推送通知的示例curl如下所示:

// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);
curl-X柱\ \ -H'授权:密钥=您的\u API\u密钥'\ -H'内容类型:应用程序/json'\ -H'缓存控制:无缓存'\ -d'{ “数据”:{ “头衔”:“头衔”, “消息”:“通知内容”, “自定义密钥”:“自定义值” }, “注册ID”:[“设备推送令牌”] }"

您还可以传递自定义键值对,并在onMessageReceived方法中获取它们

注意:在这种方法中,您必须创建在系统托盘中可见的通知。示例代码如下所示:

// Create an explicit intent for an Activity in your app
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);
您可以在


希望这有帮助!

您运行代码的android版本是什么?
Splashscreen.this.getIntent().hasExtra
是错误的。
b.getIntent().hasExtra(“值”)
Plus,如果你的应用程序已经打开,你希望它如何通过启动屏幕?@sanjeev我在创建通知时传递了一个额外的消息,因此它再次打开启动。没有问题。唯一的问题是,如果应用程序不在前台,通知活动不会打开!@rm rfstar我在android 9上运行了该应用程序,但它可以运行till API 24