[英]I'd like to count badges from ios. You can't try everything about how to do it
这是我使用的方法列表。
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(onBackgroundHandler);
}
Future onBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
FlutterAppBadger.updateBadgeCount(10);
return Future.value();
}
它不工作
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
}
它不工作
@available(iOSApplicationExtension, unavailable)
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@available(iOSApplicationExtension 10.0, *)
func didReceive(_ notification: UNNotification) {
let content = notification.request.content
let count = UIApplication.shared.applicationIconBadgeNumber
UIApplication.shared.applicationIconBadgeNumber = count + 1
if let urlImageString = content.userInfo["urlImageString"] as? String {
if let url = URL(string: urlImageString) {
URLSession.downloadImage(atURL: url) { [weak self] (data, error) in
if let _ = error {
return
}
guard let data = data else {
return
}
DispatchQueue.main.async {
self?.imageView.image = UIImage(data: data)
}
}
}
}
}
}
它不工作
@available(iOSApplicationExtension, unavailable)
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
let count = UIApplication.shared.applicationIconBadgeNumber
UIApplication.shared.applicationIconBadgeNumber = count + 1
我不想直接在 fcm 上计算徽章。 每次收到通知时如何增加徽章数量?
我知道这可能令人沮丧。 问题是您认为您需要手动和以编程方式增加徽章编号,而这不是正确的方法。
您想要的是您的通知负载实际上包含一个 integer 作为其badge
键,说明 iOS 作为操作系统要为您的应用程序显示什么徽章 integer。
此 Firebase 通知文档将为您解决问题。 因此,与其尝试手动执行此操作,不如在通知本身中包含 iOS 徽章的 integer 值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.