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
I am new to iOS and i struggled with silent push notification,googled a lot and got stuck. Will iOS awake my app when i receive silent push notification when app is not launched(i.e when app is removed from app switcher). this method is called and works fine when app is in foreground and background.
push notifications. The major difference between local and push notifications is that local notifications are scheduled by an app locally and are delivered by the same device, whereas push notifications are sent from a remote server.
It’s defined in Task.swift. You start creating a notification by populating the notification content. UNMutableNotificationContent holds the payload for a local notification. Here, you populate the title and body of the notification. UNMutableNotificationContent has other properties, such as:
In the template selector, select iOS as the platform, select the Single View App template, and then click Next. Enter SwiftUILocalNotificationTutorial as the Product Name, select SwiftUI as the User Interface and click Next. Choose a location to save the project on your Mac. An image will be used as an attachment of the notification.
Silent notifications launch the app in the background to perform an action, such as refreshing the app. You can configure both local and remote notifications using the UserNotifications framework. To learn more about remote notifications, check out Push Notifications Tutorial: Getting Started.
Enter SwiftUILocalNotificationTutorial as the Product Name, select SwiftUI as the User Interface and click Next. Choose a location to save the project on your Mac. An image will be used as an attachment of the notification. Download the image and drag it inside the project folder. In the canvas, click Resume to display the preview.
Ok I figured it out,
application.scheduleLocalNotification(localNotification)
should have been
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
Hope this helps someone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With