问题
I am learning Google cloud messaging and firebase messaging is working fine, but when a user do not click at the notification to open the app and instead goes directly to app by manually opening it and bringing to foreground, the notification stays. I want those notification message related to my app go away when the app comes to foreground. How can I achieve this ?
回答1:
It thinks there is no way to do this in documentation.
But you can try this. You can just go to your MainActivity.java in your Android folder, and do something like this.
import android.app.NotificationManager;
import android.content.Context;
Import these packages.
@Override
protected void onResume() {
super.onResume();
// Removing All Notifications
closeAllNotifications();
}
private void closeAllNotifications() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
Add an onResume
function below onCreate
on MainActivity.java.
Hope this solves your issue.
回答2:
Shri Hari solution did the trick. Kotlin:
import android.app.NotificationManager
import android.content.Context
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
override fun onResume() {
super.onResume()
closeAllNotifications();
}
private fun closeAllNotifications() {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancelAll()
}
}
来源:https://stackoverflow.com/questions/63444516/in-flutter-how-to-clear-notification-in-bar