在颤动中如何清除栏中的通知?
In flutter how to clear notification in bar?
我正在学习 Google 云消息传递并且 firebase messaging 工作正常,但是当用户没有点击通知来打开应用程序而是通过手动打开它直接进入应用程序并且带到前台,通知保留。当应用程序进入前台时,我希望与我的应用程序相关的那些通知消息消失。我怎样才能做到这一点?
它认为documentation没有办法做到这一点。
不过你可以试试这个。您可以直接转到 Android 文件夹中的 MainActivity.java,
并做这样的事情。
import android.app.NotificationManager;
import android.content.Context;
导入这些包。
@Override
protected void onResume() {
super.onResume();
// Removing All Notifications
closeAllNotifications();
}
private void closeAllNotifications() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
在 MainActivity.java 的 onCreate
下面添加一个 onResume
函数。
希望这能解决您的问题。
Shri Hari 解决方案成功了。
科特林:
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()
}
}
对于 IOS 我使用 UNUserNotificationCenter:
import UIKit
import Flutter
import UserNotifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
如果您不想修改 swift 或 java 代码,您可以使用 flutter_local_notifications
,它提供了一个方法 cancelAll()
.
class SomeWidget extends State<SomeState> with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await flutterLocalNotificationsPlugin
.cancelAll();
}
}
}
Clear/Cancel 应用加载和恢复通知
iOS AppDelegate
将您的 ios/Runner/AppDelegate.swift
替换为以下内容。
只需确保您的应用程序所需的任何代码也存在(例如 Firebase 和 FirebaseApp.configure() 的导入)。
import UIKit
import Flutter
import UserNotifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
clearAllNotifications(application)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
super.applicationDidBecomeActive(application)
clearAllNotifications(application)
}
func clearAllNotifications(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0 // Clear Badge Counts
let center: UNUserNotificationCenter = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
center.removeAllPendingNotificationRequests()
}
}
Android 主活动
注意:删除示例中的所有 <
和 >
并改用您的值。
将您的 android/app/src/main/kotlin/com/<your_domain>/<your_app_name>/MainActivity.kt
替换为以下内容。
package com.<your_domain>.<your_app_name>
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()
}
}
我正在学习 Google 云消息传递并且 firebase messaging 工作正常,但是当用户没有点击通知来打开应用程序而是通过手动打开它直接进入应用程序并且带到前台,通知保留。当应用程序进入前台时,我希望与我的应用程序相关的那些通知消息消失。我怎样才能做到这一点?
它认为documentation没有办法做到这一点。
不过你可以试试这个。您可以直接转到 Android 文件夹中的 MainActivity.java, 并做这样的事情。
import android.app.NotificationManager;
import android.content.Context;
导入这些包。
@Override
protected void onResume() {
super.onResume();
// Removing All Notifications
closeAllNotifications();
}
private void closeAllNotifications() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
在 MainActivity.java 的 onCreate
下面添加一个 onResume
函数。
希望这能解决您的问题。
Shri Hari 解决方案成功了。 科特林:
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()
}
}
对于 IOS 我使用 UNUserNotificationCenter:
import UIKit
import Flutter
import UserNotifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests()
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
如果您不想修改 swift 或 java 代码,您可以使用 flutter_local_notifications
,它提供了一个方法 cancelAll()
.
class SomeWidget extends State<SomeState> with WidgetsBindingObserver {
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await flutterLocalNotificationsPlugin
.cancelAll();
}
}
}
Clear/Cancel 应用加载和恢复通知
iOS AppDelegate
将您的 ios/Runner/AppDelegate.swift
替换为以下内容。
只需确保您的应用程序所需的任何代码也存在(例如 Firebase 和 FirebaseApp.configure() 的导入)。
import UIKit
import Flutter
import UserNotifications
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
clearAllNotifications(application)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
super.applicationDidBecomeActive(application)
clearAllNotifications(application)
}
func clearAllNotifications(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0 // Clear Badge Counts
let center: UNUserNotificationCenter = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
center.removeAllPendingNotificationRequests()
}
}
Android 主活动
注意:删除示例中的所有 <
和 >
并改用您的值。
将您的 android/app/src/main/kotlin/com/<your_domain>/<your_app_name>/MainActivity.kt
替换为以下内容。
package com.<your_domain>.<your_app_name>
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()
}
}