アプリがバックグラウンドであっても通知が届いていますが、通知が届くたびに自動的にアクティビティが開きます(ユーザーが通知をクリックしなくても)。FCM Androidのデータメッセージ
マイonMessageReceived
が
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle b = new Bundle();
if (data.containsKey("click_action")) {
if(data.containsValue("postlike") || data.containsValue("comment") || data.containsValue("newpost")){
b.putString("PostId", data.get("post_id").toString());
Firebase_Action_Click.startActivity(data.get("click_action"), b, this);
} else {
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
}
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentText(data.get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
であると私Firebase_Action_Click
public static void startActivity(String className, Bundle extras, Context context) {
Class cls = null;
try {
cls = Class.forName(className);
} catch(ClassNotFoundException e){
//means you made a wrong input in firebase console
}
Intent i = new Intent(context, cls);
i.putExtras(extras);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
そして、私のランチャー活動の中で、私はこれらの行を追加した、
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra("click_action")) {
Firebase_Action_Click.startActivity(intent.getStringExtra("click_action"), intent.getExtras(), this);
}
}
すべてがうまく行くが、唯一の問題私が直面しているのは、アプリがバックグラウンドにあり、通知があったとしてもアプリケーションを自動的に開きます。
ユーザーが通知をクリックしたときにのみ、同じ操作を実行する必要があります。
誰もがこの上で私を助けてもらえ、事前
この行は何をすべきですか? Firebase_Action_Click.startActivity(data.get( "click_action")、b、this); – Mushirih