我試圖在我的應用處於前臺時顯示本地通知。我在顯示遠程通知時沒有問題,但是當應用程序在前臺運行時遇到問題。我只有新的iOS 10的問題。xcode 8和iOS 10本地通知
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// TODO: Handle data of notification
if application.applicationState == UIApplicationState.Active {
//print("Message ID: \(userInfo["gcm.message_id"]!)")
//print("Message ID: \(userInfo.keys)")
dispatch_async(dispatch_get_main_queue(), {() -> Void in
if (userInfo["notice"] != nil) {
if #available(iOS 10.0, *) {
print ("yes")
let content = UNMutableNotificationContent()
content.title = "My Car Wash"
content.body = (userInfo["notice"] as? String)!
}
else
{
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow:0)
localNotification.alertBody = userInfo["notice"] as? String
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertAction = nil
localNotification.timeZone = NSTimeZone.defaultTimeZone()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
let systemSoundID: SystemSoundID = 1000
// to play sound
AudioServicesPlaySystemSound (systemSoundID)
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
completionHandler(.NewData)
}
}
})}
}
我的iPhone運行iOS 10,我可以看到「是」被打印出來。我的應用程序具有必需的通知權限。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Register for remote notifications
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
// [END register_for_notifications]
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
正如在iOS 9設備上提到的,代碼工作正常,當應用程序未運行時我收到通知。當應用處於前臺時,問題出現在iOS 10上。我一直在尋找谷歌一段時間,但我仍然不在那裏。任何幫助或建議將不勝感激。
這應該對你有幫助:https://makeapppie.com/2016/08/08/how-to-make-local-notifications-in-ios-10/#comments – Do2
用Objective-C方法http:// stackoverflow。 COM /問題/ 37938771/uilocalnotification-被棄用的功能於ios10/37969401#37969401 – ElonChan