首页 > 解决方案 > ios App本地通知未在生产中显示通知

问题描述

这是我的 AppDelegate.swift 文件代码:

我在这里调用了 showNotification():

override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {

    //call channel handler
    controller = window?.rootViewController as? FlutterViewController
    callChannel = FlutterMethodChannel(name: "com.nta.ntacall/calls",
                                              binaryMessenger: controller.binaryMessenger)
    callChannel.setMethodCallHandler({
        (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
        // Note: this method is invoked on the UI thread.
        guard call.method == "processCalls" else {
            result(FlutterMethodNotImplemented)
            return
        }
        //process call test
        //self.processCalls()
    })

    GeneratedPluginRegistrant.register(with: self)
    callObserver.setDelegate(self, queue: nil)
    UNUserNotificationCenter.current().delegate = self
    getLocation()
    /calling the show notification method here
    showNotification()
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}

我在这里有这个 showNotification 方法:

func showNotification(){
    let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    }
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    }
    //if not active state then show notification
    let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }else if state == .active {
        print("App in Foreground or Active")
    }

    if state == .background {
        print("1")
        
        print("CXCallState :Disconnected")
        
        // Step 2: Create the notification content
        let content = UNMutableNotificationContent()
        content.title = "Please rate your last call"
        content.body = "Tap to rate..."
        //show notification
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
        // Step 5: Register the request
        center.add(request) { (error) in
        // Check the error parameter and handle any errors
        }
    }
    else{
        processCalls()
    }
    
}

它在开发、模拟器和真实设备中运行良好,但在应用程序商店中发布应用程序时没有显示通知?可能是什么原因?

标签: iosswiftflutter

解决方案


推荐阅读