即使应用程序在后台,我也会收到通知消息,但只要收到通知,它就会自动打开 Activity (即使用户没有点击通知)。
我的 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);
}
在我的启动器 Activity 中,我添加了这些行,
@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);
}
}
一切顺利,但我面临的唯一问题是,即使该应用程序在后台运行并且我收到通知它会自动打开该应用程序。
我希望仅当用户单击通知时才执行相同的操作。
谁能帮我解决这个问题,在此先感谢
最佳答案
它会触发 Activity
,因为您从 onMessageReceived
函数启动了 Activity
。因此,如果您想在单击通知时启动 Activity
,您可能需要启动 Activity
作为 PendingIntent
。
我想推荐对您的代码进行简单的修改。 Firebase_Action_Click
中的 startActivity
函数可能会返回它创建的 Intent
,如下所示。
public static Intent 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);
return i; // Return the intent from here.
}
现在在您的 onMessageReceived
函数中,您需要在 if(data.containsValue("poSTLike") || data.containsValue( "comment") || data.containsValue("newpost"))
- 这个 if 语句,就像你为 else
部分设置的那样。所以你的代码可能看起来像这样。
@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());
Intent intent = Firebase_Action_Click.startActivity(data.get("click_action"), b, this);
// Now add the flags and create pendingIntent here
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
} 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());
}
现在您的通知系统应该可以正常工作了。
关于android - FCM 安卓数据报文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996829/