简体   繁体   中英

unable to receive local notification without internet iOS swift

I'm scheduling a local notification on the moment I receive a remote notification by using the below code,

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

After successfully scheduling local notification. Now, I turned off internet, I'm not receiving local notification..

Did anyone face the same issue? or Am I missing something?

For Local Notifications write this code in your appDelegate's didFinishLaunchingWithOptions :-

let notifyTypes:UIUserNotificationType = [.Alert, .Badge, .Sound]
let settings = UIUserNotificationSettings(forTypes: notifyTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

// and Schedule your notifications in your controller :-

let notification:UILocalNotification = UILocalNotification()
notification.alertTitle = " "
notification.alertBody = ""
notification.fireDate = NSDate()               
UIApplication.sharedApplication().scheduleLocalNotification(notification)

you are calling the push notification receive function. you have to call didReceiveNotification only.

And need to schedule local notification from your controller not in this function.

Hope this will help you.

You have to set this code on viewWillAppear or button click event.

Not on didReceiveRemoteNotification or didReceiveLocalNotification.

Try this code on viewwillAppear or button

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)

Now once you receive local notification, when you tap on that, didReceiveLocalNotification method will get call in AppDelegate

If you want to fire local notification instantly(without scheduling) then you need the following code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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