Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to run some code when a local notification is sent?

I want to run some code to update the viewcontroller when my local notification has been sent out, I have tried the function didReceiveLocalNotification, but it only works when the user clicks on the notification, I was wondering if there was a way to run a function when the notification has been sent out?

like image 945
Ludvig Sørensen Avatar asked Feb 11 '16 22:02

Ludvig Sørensen


People also ask

Do local notifications work when app is closed?

The local notification should occur at the schedule time even if the app is closed.

What is the difference between local notification and push notification?

The essential difference between local notifications and push notifications is simple: Local notifications are scheduled by an app locally and are delivered by the same device. Push notifications are sent by a remote server (its provider) which sends these notifications to devices on which the app is installed.

Can you send local notifications while app is in background?

Notifications could be created at any moment (on Foreground, Background or even when the application is terminated/killed).

What is remote notification in IOS?

Overview. Use remote notifications (also known as push notifications) to push small amounts of data to devices that use your app, even when your app isn't running. Apps use notifications to provide important information to users. For example, a messaging service sends remote notifications when new messages arrive.


1 Answers

I think there is no way you can know when your local notification is sent out mainly because you can schedule your local notification or send it right away in case of your app are in background.

According to Apple:

You schedule a local notification by calling scheduleLocalNotification:. The app uses the fire date specified in the UILocalNotification object for the moment of delivery. Alternatively, you can present the notification immediately by calling the presentLocalNotificationNow: method.

Apps might also find local notifications useful when they run in the background and some message, data, or other item arrives that might be of interest to the user. In this case, an app can present the notification immediately using the UIApplication method presentLocalNotificationNow: (iOS gives an app a limited time to run in the background).

There is no way to know when the UILocalNotification was sent, but it when it has received is some case of course. In your case I suppose you're scheduling a UILocalNotification so let's divided by parts:

  • The notification is delivered when the app isn’t open and the user touch it

    The delegate for an iOS app implements the application:didFinishLaunchingWithOptions: method to handle a local notification. It gets the associated UILocalNotification object from the launch-options dictionary using the UIApplicationLaunchOptionsLocalNotificationKey key. So you can know where the app was open by touching the UILocalNotification. See the following code:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        if let launchNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
            // do anything you want update UIViewController, etc
        }
    
        return true
    } 
    
  • The notification is delivered when the app is running and the user touch it

    Once the user touch the UILocalNotification you can handle it using the didReceiveLocalNotification method and do whatever you want inside it.

  • The notification is delivered when the app is running and the user don't touch it

In case the user don't touch the UILocalNotification the only thing you can do to know if your UILocalNotification was sent is keep a list with the UILocalNotifications you have scheduled before and check it later(this is not a excellent solution but its work).

Nevertheless always exist the UIApplicationWillEnterForegroundNotification that can be used to notify the UIViewController you want to update that the app will enter in foreground like in this way:

private var foregroundNotification: NSObjectProtocol!

override func viewDidLoad() {
    super.viewDidLoad()

    foregroundNotification = NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue()) {
        [unowned self] notification in

        print("Notified ViewControllerB")
    }
}

deinit {
    // make sure to remove the observer when this view controller is dismissed/deallocated

    NSNotificationCenter.defaultCenter().removeObserver(foregroundNotification)
}

I think you have to think properly what you need to do inside you app when your notification is received in case of the user don't tap it.

I hope this help you.

like image 90
Victor Sigler Avatar answered Oct 20 '22 22:10

Victor Sigler