firebase 通知でヘルプが必要です。
私のアプリでは、アプリ ユーザーにプッシュ通知を送信するために FCM を使用しています。
Postman を使用して、アプリにデータ通知を送信しています。ネットを見回した後、アプリでデータ通知の処理を行ったので、通知を通じて送信されたパラメーターに基づいて、さまざまなデータを表示したり、さまざまなアクティビティを開いたりできます。
しかし、データ通知を使用してfirebaseコンソールを使用して同じことをしようとすると、ランチャーアクティビティを通じてアプリを開くだけです。
ここで奇妙なことが起こっていますが、私には理解できません。
POSTMAN を使用: アプリがフォアグラウンドにあるときに Postman を使用して通知を送信すると、targetActivity がトリガーされますが、バックグラウンドでアプリと同じことをしようとすると、通知が表示されますが、通知をクリックすると、単にトレイから離れて何もしません。
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."
}
}
Android アプリでの onMessageReceived
{
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) を効率的に処理するのに役立つ人がいる場合は、そうしてください。