在firebase通知中需要帮助 .

在我的应用程序中,我使用FCM向我的应用程序用户发送推送通知 .

使用Postman我正在向我的应用发送数据通知 . 在网上浏览后,我在我的应用程序中完成了数据通知处理,现在我可以根据通过通知发送的参数显示不同的数据或打开不同的活动 .

但是,当我尝试使用带有数据通知的firebase控制台时,它只需通过luncher活动打开应用程序 .

奇怪的事情发生在这里,但我无法弄明白 .

With POSTMAN: 当应用程序在前台时使用Postman发送通知会触发targetActivity但是当我尝试在后台使用app时,我会收到通知但是点击通知只是远离托盘而什么都不做 .

With Firebase Console 通过FCM Concole发送通知只会打开启动器活动而不是targetActivity . 如果应用程序位于前台或后台,则无关紧要 .

这是我的代码

邮递员要求:

{
"to" : "cwDBStUSeB-MY-Firebase-Token-JOLscpe_6OWQoWnounUhw_trlY5Qw8w",

 "notification" : {
        "click_action" : ".SurveySlider", // i'm not using this 
        "body" : "Complete the survey and get bonus points.", 
        "title" : "Survey", 
         }, 
 "data": {  "transactionID" : "123456",
    "openActivity" : "Surveys",
    "message" : "This is message from data set."

 }
}

onMessageRecieved在我的Android应用程序中

{
        Intent intent = new Intent(this, BaseDrawerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        intent.putExtra("transactionID", remoteMessage.getData().get("transactionID"));
        intent.putExtra("targetActivity", remoteMessage.getData().get("openActivity"));

//        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
//            manager.createNotificationChannel(channel);
//        }

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(BaseDrawerActivity.class);

        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(intent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
       // builder.setContentIntent(resultPendingIntent);

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



        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "Default";
        NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_logo_circle)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        int id = new AtomicInteger(0).incrementAndGet();
        manager.notify(id, builder.build());
}

BaseDrawerActivity根据通知数据处理不同的目标活动 .

Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            if (bundle.containsKey("targetActivity")) {
                String target = bundle.getString("targetActivity");    
                if (target.equalsIgnoreCase("Transactions")) {

                    MyActivity _myActivity = new MyActivity();
                    Bundle args = new Bundle();
                    args.putString("transactiontype", "all");
                    _myActivity.setArguments(args);
                    _fragmentTransaction = getSupportFragmentManager().beginTransaction().replace(R.id.content_base_drawer, _myActivity);
                    current = R.id.nav_Activity;
                    _fragmentTransaction.commit();


                } else if (target.equalsIgnoreCase("Surveys")) {
                    if (bundle.containsKey("transactionID")) {
                        String TID = bundle.getString("transactionID");
                        startActivity(new Intent(BaseDrawerActivity.this, SurveySlider.class).putExtra("Transaction_ID", TID));
                    } else {
                        Toast.makeText(getApplicationContext(), getText(R.string.transactionIDmissing), Toast.LENGTH_SHORT).show();
                    }
                }else{
                    navigationView.getMenu().performIdentifierAction(R.id.nav_OverView, 0);
                    current = R.id.nav_OverView;
                    navigationView.getMenu().getItem(0).setChecked(true);
                }

            }else{

                    navigationView.getMenu().performIdentifierAction(R.id.nav_OverView, 0);
                current = R.id.nav_OverView;
                navigationView.getMenu().getItem(0).setChecked(true);

            }

        }
        else
            Log.d(TAG, "Bundle is NULL");
}

请帮我 .

PS我的应用程序基于带有片段的Navigationdrawer . 因此,如果有些人可以帮助我根据目标有效载荷中的目标有效地处理TargetActivites(片段),请执行此操作 .