繁体   English   中英

没有互联网iOS swift无法接收本地通知

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM