puz*_*led 8 android-notifications flutter firebase-cloud-messaging
我正在学习 Google 云消息传递和firebase 消息传递工作正常,但是当用户没有单击通知打开应用程序而是通过手动打开它并置于前台直接进入应用程序时,通知会保留。当应用程序进入前台时,我希望那些与我的应用程序相关的通知消息消失。我怎样才能做到这一点?
Gal*_*Rom 11
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()
}
}
Run Code Online (Sandbox Code Playgroud)
对于 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)
}
}
Run Code Online (Sandbox Code Playgroud)
它认为在文档中没有办法做到这一点。
但是你可以试试这个。您可以直接转到 Android 文件夹中的 MainActivity.java,然后执行类似操作。
import android.app.NotificationManager;
import android.content.Context;
Run Code Online (Sandbox Code Playgroud)
导入这些包。
@Override
protected void onResume() {
super.onResume();
// Removing All Notifications
closeAllNotifications();
}
private void closeAllNotifications() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
Run Code Online (Sandbox Code Playgroud)
在 MainActivity.javaonResume下面添加一个函数onCreate。
希望这能解决您的问题。
| 归档时间: |
|
| 查看次数: |
2843 次 |
| 最近记录: |