Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 968
Gmeister4 Avatar asked Mar 24 '15 15:03

Gmeister4


People also ask

Will iOS awake my app when I receive silent push notification?

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.

What is difference between push notification and local notification?

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.

What is unmutablenotificationcontent in Swift?

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:

How to create a notification using swiftuilocalnotificationtutorial?

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.

What is the use of silent notifications?

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.

How to create a swift UI notification on Mac?

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.


Video Answer


1 Answers

Ok I figured it out,

application.scheduleLocalNotification(localNotification)

should have been

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

Hope this helps someone!

like image 76
Gmeister4 Avatar answered Nov 14 '22 23:11

Gmeister4