[英]unable to receive local notification without internet iOS swift
我正在通過使用以下代碼收到遠程通知的時刻安排本地通知,
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
let scheduleLocalNotification = UILocalNotification()
scheduleLocalNotification.fireDate = dateFromRemoteNotification
scheduleLocalNotification.timeZone = NSTimeZone.localTimeZone()
scheduleLocalNotification.alertBody = "Hi There!"
scheduleLocalNotification.userInfo = userInfo
UIApplication.sharedApplication().scheduleLocalNotification(scheduleLocalNotification)
completionHandler(.NewData)
}
成功調度本地通知后。 現在,我關閉了互聯網,我沒有收到本地通知..
有沒有人面臨同樣的問題? 還是我錯過了什么?
對於本地通知,請在appDelegate的didFinishLaunchingWithOptions中寫下此代碼: -
let notifyTypes:UIUserNotificationType = [.Alert, .Badge, .Sound]
let settings = UIUserNotificationSettings(forTypes: notifyTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
//並在您的控制器中安排通知: -
let notification:UILocalNotification = UILocalNotification()
notification.alertTitle = " "
notification.alertBody = ""
notification.fireDate = NSDate()
UIApplication.sharedApplication().scheduleLocalNotification(notification)
您正在調用推送通知接收功能。 你必須只調用didReceiveNotification。
並且需要安排來自您的控制器的本地通知而不是此功能。
希望這會幫助你。
您必須在viewWillAppear或按鈕單擊事件上設置此代碼。
不在didReceiveRemoteNotification或didReceiveLocalNotification上。
在viewwillAppear或按鈕上嘗試此代碼
let notification = UILocalNotification()
notification.alertBody = "Local notification text body"
notification.alertAction = "open"
notification.fireDate = NSDate(timeIntervalSinceNow: 1) // this will fire local notification just after 1 second
notification.soundName = "soundName.mp3"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
現在一旦你收到本地通知,當你點擊它時,didReceiveLocalNotification方法將在AppDelegate中調用
如果您想立即觸發本地通知(無需安排),那么您需要以下代碼:
let locNot:UILocalNotification = UILocalNotification();
locNot.alertBody = "Here is the local notification";
UIApplication.sharedApplication().presentLocalNotificationNow(locNot);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.