更换FCM通知使用我已切换到使用FCM我的应用程序创建的通知

问题描述:

。当我的应用程序打开时,我手动处理的消息,如果没有显示包含邮件列表中的Fragment,那么我的代码显示一个Notification。要显示Notification我使用的功能:更换FCM通知使用我已切换到使用<code>FCM</code>我的应用程序创建的通知

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创建程序可以取代通过FCM创建Notification

那是因为你收到FCM通知消息:

通知邮件传递到通知栏,当 应用程序是在后台。在前台应用程序,消息 由这些回调处理:

didReceiveRemoteNotification:Android上的iOS
onMessageReceived()。

您会收到FCM数据消息,而不是和编程创建通知:

客户端应用程序负责处理数据消息。

Documentation

请注意,FCM处理与纯NotificationData有效载荷不同的消息。请检查您的消息,看看您发送的是哪种类型的文件,并检查Firebase文档。

我有,当我不得不运行我不想收到通知某活动的问题。所以我创建了一个静态实用程序类,它保存了一个布尔属性来指示该Activity是否处于活动状态。在我的Notification接收器中,我检查这个值并发布通知或不通知。就像这样:

我的静态工具类:

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()); 
    } 
} 

有可能是在这个例子中,你不需要一些事情。