Flutter:我无法以编程方式删除传入的 Firebase 通知

Fab*_*aro 6 android firebase flutter firebase-cloud-messaging

我的手机收到了一条传入的 Firebase 通知,如果用户没有点击它并且用户重新打开应用程序,我希望通过从通知有效负载收到的标签和 id_notification 以编程方式删除 Android 通知。

我使用了 Firebase_messaging "^1.0.1" Flutter 插件和 MessageHandler 来处理传入消息。

为了删除通知,我使用了平台渠道:

private boolean removeNotification(String tag, int idNotifica) {
    boolean result = false;
    NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.cancel(tag, idNotifica);
    result = true;
    return result;
  }
Run Code Online (Sandbox Code Playgroud)

但它不起作用,或者更确切地说,通知无法删除并且发生 PlatformException。

当我的手机收到 Firebase 通知后,我运行此平台通道代码来查看 StatusBarNotification:

  @RequiresApi(api = Build.VERSION_CODES.M)
  public String  getInfoAllNotification() {
    String info = "";
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    StatusBarNotification[] barNotifications = 
notificationManager.getActiveNotifications();
    for(StatusBarNotification notificationInStatusBar: barNotifications) {
      Notification n = notificationInStatusBar.getNotification();
      Bundle b = n.extras;
      if (b != null) {
        for (String key : b.keySet()) {
          Object value = b.get(key);
          info = info + String.format("%s %s ; ", key, value!=null?value.toString():"");
        }
        info = info + String.format("id_notification %s ;", notificationInStatusBar.getId());
        info = info + String.format("key %s ;", 
notificationInStatusBar.getKey());
        info = info + String.format("groupKey %s ;",notificationInStatusBar.getGroupKey());
        info = info + String.format("tag %s ;",notificationInStatusBar.getTag());
        info = info + String.format("postTime %s ;", notificationInStatusBar.getPostTime());
      }
    }
    return info;
  }
Run Code Online (Sandbox Code Playgroud)

我注意到 id_notification 始终为 0,如下所示:

   FIRST NOTIFICATION
android.title this is a title ; 
android.subText  ; 
android.template android.app.Notification$BigTextStyle ; 
android.showChronometer false ; 
android.icon Icon(typ=RESOURCE pkg=com.XXX.firebase id=0x7f090000) ; 
android.text this is a body ; 
android.progress 0 ; 
android.progressMax 0 ; 
android.showWhen true ; 
android.rebuild.applicationInfo ApplicationInfo{23547cf com.XXX.firebase} ; 
android.rebuild.contentView true ; 
android.bigText this is a body ; 
android.infoText  ; 
android.originatingUserId 0 ; 
android.progressIndeterminate false ; 
android.rebuild true ; 
android.rebuild.bigView true ; 
id_notification 0 ;
key 0|com.XXX.firebase|0|2|10059 ;groupKey 0|com.XXX.firebase|0|2|10059 ;
tag 2 ;
postTime 1530639629519 ;

    SECOND NOTIFICATION
android.title this is a title ; 
android.subText ; 
android.template android.app.Notification$BigTextStyle ; 
android.showChronometer false ; 
android.icon Icon(typ=RESOURCE pkg=com.XXX.firebase id=0x7f090000) ; 
android.text this is a body ; 
android.progress 0 ; 
android.progressMax 0 ; 
android.showWhen true ; 
android.rebuild.applicationInfo ApplicationInfo{5aad55c com.XXX.firebase} ; 
android.rebuild.contentView true ; 
android.bigText this is a body ; 
android.infoText  ; 
android.originatingUserId 0 ; 
android.progressIndeterminate false ; 
android.rebuild true ; 
android.rebuild.bigView true ; 
id_notification 0 ;
key 0|com.XXX.firebase|0|1|10059 ;
groupKey 0|com.XXX.firebase|0|1|10059 ;
tag 1 ;
postTime 1530639593190 ;
Run Code Online (Sandbox Code Playgroud)

请帮我。提前致谢。