没有internet iOS swift,无法接收本地通知

没有internet iOS swift,无法接收本地通知,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,我正在使用下面的代码在收到远程通知时安排本地通知 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { let scheduleLocalNotification

我正在使用下面的代码在收到远程通知时安排本地通知

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)

对于本地通知,请在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

并且需要安排来自控制器的本地通知,而不是此功能


希望这对您有所帮助。

您正在调用推送通知接收功能。您只需拨打didReceiveNotification

并且需要安排来自控制器的本地通知,而不是此功能


希望这将对您有所帮助。

您必须在ViewWillDisplay或按钮单击事件上设置此代码

不在DidReceiveMemoteNotification或didReceiveLocalNotification上

在ViewWillDisplay或按钮上尝试此代码

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中获得调用

您必须在ViewWillDisplay或按钮单击事件上设置此代码

不在DidReceiveMemoteNotification或didReceiveLocalNotification上

在ViewWillDisplay或按钮上尝试此代码

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);

如果要立即触发本地通知(无需计划),则需要以下代码:

let locNot:UILocalNotification = UILocalNotification();
locNot.alertBody = "Here is the local notification";
UIApplication.sharedApplication().presentLocalNotificationNow(locNot);