繁体   English   中英

我想从 ios 开始计算徽章。你不能尝试所有方法

[英]I'd like to count badges from ios. You can't try everything about how to do it

这是我使用的方法列表。

  1. 在后台处理程序上使用 firebase。
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();
}

它不工作

  1. Code AppDelegate 直接编码。
@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
        
    }
}

它不工作

  1. 直接编码添加到通知服务扩展的代码。

@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)
                    }
                }
            }
        }
    }
    
}

它不工作

  1. 编码直接将代码添加到 Notification controller 扩展。

@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
        

https://pushpsenairekar.medium.com/increment-ios-apps-badge-count-in-using-5-simple-steps-3d80e77d45c8参考博客。

我不想直接在 fcm 上计算徽章。 每次收到通知时如何增加徽章数量?

我知道这可能令人沮丧。 问题是您认为您需要手动和以编程方式增加徽章编号,而这不是正确的方法。

您想要的是您的通知负载实际上包含一个 integer 作为其badge键,说明 iOS 作为操作系统要为您的应用程序显示什么徽章 integer。

此 Firebase 通知文档将为您解决问题。 因此,与其尝试手动执行此操作,不如在通知本身中包含 iOS 徽章的 integer 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM