即使應用程序位於後臺,我也會收到通知消息,但每當收到通知時,都會自動打開該活動(即使用戶未單擊該通知)。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