这是我的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()
}
}
在开发中以及在模拟器和真实设备中都可以正常工作,但是在应用商店中发布该应用时它没有显示通知吗? 可能是什么原因?