ios swift creating local notification after silent notification
Asked Answered
C

1

6

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

Cyrenaic answered 24/3, 2015 at 15:13 Comment(0)
C
7

Ok I figured it out,

application.scheduleLocalNotification(localNotification)

should have been

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

Hope this helps someone!

Cyrenaic answered 26/3, 2015 at 10:38 Comment(6)
Hi , I can't get this working , Im doing same settings like yours but still can't get local notification fired when silent notification arrives while App is in not running state.Would you please suggest anything for this .Thanks in advance.Meurer
Hi, if your app is killed you wont receive remote notifications. You need to use VoIP notifications when your app is killed. Unfortunately Apple only let you use VoIP notification for specific purposes.Cyrenaic
developer.apple.com/library/content/documentation/Performance/…Cyrenaic
@Meurer statring a background task after receiving the silent push, and then firing the local notification will wokr for you.Neckwear
@RajaSaad, yes, I cant call details of this, its long time ago, but I do believe as a concept if you want to do some upon receiving push, then fire your own local, would be via VoIP push-type from developer portal, that allows receiving silent pushes, also I think normal push notification with some modification to the payload, could be received without alert, lemme know if want to dig together in this, wish I can help!Meurer
@Meurer but with VOIP PSUH, you'll have to engage the Callkit as well.Neckwear

© 2022 - 2024 — McMap. All rights reserved.