0

在 firebase 通知中需要帮助。

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

使用邮递员我正在向我的应用程序发送数据通知。在网上看了一圈后,我已经在我的应用程序中完成了数据通知处理,现在我可以根据通过通知发送的参数显示不同的数据或打开不同的活动。

但是当我尝试使用带有数据通知的firebase控制台来做同样的事情时,它只是通过午餐活动打开应用程序。

这里发生了一些奇怪的事情,但我无法弄清楚。

使用 POSTMAN: 当应用程序处于前台时使用 Postman 发送通知会触发 targetActivity 但是当我尝试在后台对应用程序执行相同操作时,我会收到通知,但单击通知只会离开托盘并且什么也不做。

使用 Firebase 控制台 通过 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(Fragments),请这样做。

4

0 回答 0