当应用程序不在前台时(通知是最新的,未终止!),通知通知中没有打开特定活动

时间:2019-11-20 05:17:46

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

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

(这是我执行网络任务后调用的函数:)

        if (list.size()!=0){
            for (int i=0;i<list.size();i++){
                Users u=list.get(i);
                //Toast.makeText(LoginActivity.this, "Logging in...", Toast.LENGTH_SHORT).show();
                final Intent intent;
                Bundle b=new Bundle();
// This is the solution i found to check whether the extras has the package name or not! But it doesnt seem to work.
                if (Splashscreen.this.getIntent().getExtras() != null){
                    if (Splashscreen.this.getIntent().hasExtra("pushnotification") || Splashscreen.this.getIntent().getExtras().containsKey("com.tracecost")){
                        System.out.println("From notification----------->");
                        intent=new Intent(Splashscreen.this,NotificationReceivedActivity.class);
                        b.putString("pushnotification","yes");
                    }
                    else{
                        intent=new Intent(Splashscreen.this, ProjectSelection.class);
                    }
                }
                else{
                    intent=new Intent(Splashscreen.this, ProjectSelection.class);
                }

                b.putSerializable("user",u);
                b.putSerializable("projectlist",plist);
                intent.putExtras(b);
                //logginDialog.dismiss();
                Handler handler=new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(intent);
                        finish();
                    }
                },3500);

            }
        }
    }```

3 个答案:

答案 0 :(得分:0)

这仅在应用程序处于前台时有效。当应用程序在后台运行时,它仅考虑有效负载中的通知数据,而忽略数据部分。这将导致无法控制应用程序,因为通知类型的消息将仅由系统处理。唯一的选择是使用您自己的外部服务器或从其他客户端发送。

答案 1 :(得分:0)

当应用程序位于“前景”或“背景”中时,您可以从Firebase通知中重定向至您发送的data

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

  

附言::如果您想处理已经在后台打开的活动,请使用taskAffinity进行播放。

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

答案 2 :(得分:0)

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

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

用于触发推送通知的示例卷曲看起来像:

  

卷曲-X POST \     https://fcm.googleapis.com/fcm/send \     -H'授权:密钥= YOUR_API_KEY'\     -H'内容类型:application / json'\     -H'cache-control:no-cache'\     -d'{       “数据”:{           “ title”:“ TITLE”,           “ message”:“通知内容”,         “ custom_key”:“ custom_value”        },       “ registration_ids”:[“ DEVICE_PUSH_TOKEN”]   }'

您也可以传递自定义键值对,并将其通过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);

您可以在official documentation

中找到更多信息

希望这会有所帮助!