我已切换到使用FCM
作为我的应用程序。当我的应用程序打开时,我手动处理这些消息,如果没有显示包含消息列表的Fragment
,那么我的代码显示为Notification
。要显示Notification
我使用函数:
public void notify(int id, Notification notification)
我遇到的问题是,如果我的应用程序在后台,FCM
会显示Notification
。我甚至在服务器上设置了tag
参数,因此只为应用程序显示一个Notification
。如果用户在未单击Notification
的情况下打开应用程序,然后收到消息,则会显示单独的Notification
,这不是我想要的。我切换到使用功能:
public void notify(String tag, int id, Notification notification)
使用服务器用于tag
消息的FCM
仍然会产生第二个通知。我以编程方式创建的Notification
是否可以替换Notification
创建的FCM
?
答案 0 :(得分:1)
请注意,FCM处理纯Notification
或Data
个有效负载的邮件的方式不同。请检查您的邮件,了解您要发送的邮件类型并查看Firebase文档。
我遇到了运行某个Activity时我不想收到通知的问题。所以我创建了一个静态实用程序类,它保留了一个布尔属性,指示该Activity是否处于活动状态。在我的通知接收器中,我检查此值并发出通知或不发出通知。像这样:
我的静态实用程序类:
public class MyRunningActivity {
private static boolean isActivityRunning = false;
public static void setIsRunningActivity(boolean isRunning){
isActivityRunning = isRunning;
}
public static boolean getIsRunningActivity(){
return isActivityRunning;
}
}
在收到onMessageReceived
:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String notification = "";
String title = "";
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
title = getDataWithKey(remoteMessage.getData(), "title");
notification = getDataWithKey(remoteMessage.getData(), "body");
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
notification = remoteMessage.getNotification().getBody();
title = remoteMessage.getNotification().getTitle();
}
sendNotification(title, notification);
}
private void sendNotification(String notificationTitle, String notificationBody) {
try{
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NO_ANIMATION);
//Generate a new 'unique' request code -- this helps to refresh the activity if it is already active
int requestCode = CodeGenerator.getRandomNumber(10, 10000);
PendingIntent pendingIntent = null;
// If the Activity is active then this will keep the notification from being sent
// But the intent Extras will be delivered to the OnNewIntent method and handled there.
// I had to put the singleTop flag in the Manifest, otherwise this will cause the
// activity to close and reopen....
try {
if(MyRunningActivityUtility.getIsRunningActivity()) {
intent.putExtra("add_ring_tone","true");
pendingIntent = PendingIntent.getActivity(this,
requestCode,
intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT
);
pendingIntent.send();
\\ !!! Notice that return here prevents the Notification from being sent !!!
return;
}
}
}
catch (Exception ex1){
Log.e(TAG, ex1.getMessage());
}
pendingIntent = PendingIntent.getActivity(this,
requestCode,
intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT
);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (android.support.v7.app.NotificationCompat.Builder)
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.your_custom_icon)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int idNot = CodeGenerator.getRandomNumber(10, 10000);
notificationManager.notify(idNot, notificationBuilder.build());
}
catch (Exception ex){
Log.e(TAG, ex.getMessage());
}
}
在这个例子中可能有一些你不需要的东西。
答案 1 :(得分:0)
那是因为您收到了FCM通知消息:
通知邮件在发送到通知托盘时 应用程序在后台。对于前台的应用程序,消息是 由这些回调处理:
didReceiveRemoteNotification:在iOS上 Android上的onMessageReceived()。
您应该接收FCM数据消息并以编程方式创建通知:
客户端应用程序负责处理数据消息。