Search code examples
iosiphoneswiftapple-push-notificationsapns-php

ios swift creating local notification after silent notification


I am sending a silent push notification to my app, then processing the content of the push notification, before deciding whether to send a local notification to the user. However I cant fire a local notification from the app after receiving a silent notification.

Here is my code in appdelegate after triggering a silent notificaiton:

func application(application: UIApplication,
    didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
    fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
        println("Did receive remote with completion handler")

        var localNotification:UILocalNotification = UILocalNotification()
        localNotification.alertAction = "view broadcast messages"
        localNotification.alertBody = "Hello"
        localNotification.fireDate = NSDate(timeIntervalSinceNow: 0)

        localNotification.soundName = UILocalNotificationDefaultSoundName;

        application.scheduleLocalNotification(localNotification)

        handler(UIBackgroundFetchResult.NewData)
}

However the local notification never fires.

I have enabled Location updates, background fetch and remote notifications capabilities.

Also, when my app is in it's background state it listens for location updates then alerts the user with a local notification in specific situations- which works from similar code.

So why do local notifications not fire from didReceiveRemoteNotification?

Thanks


Solution

  • Ok I figured it out,

    application.scheduleLocalNotification(localNotification)
    

    should have been

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
    

    Hope this helps someone!