In flutter how to clear notification in bar?

流过昼夜 提交于 2021-01-05 08:46:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!