iPhone Tutorial: Scheduling Local Notifications using a Singleton class

In iOS 4, Apple introduced a new way to send “push notifications” to your app without having a dedicated server and associated server side coding. The technique is called as scheduling local notifications. While this tutorial is about how to schedule local notifications, I would also explain how a powerful abstraction concept, Singleton design pattern could make this easier when adding local notification support to your app.

If you were following my blog, you might have already read about the singleton xcode template I wrote about previously. If not, go ahead and download the Singleton class template. Install it into your xcode installation folder (which is normally /Developer). Come back here.

This class is the singleton method’s sole constructor. This sharedInstance is accessed your view or any other class that requires this class’s object.

In this class we write code for checking if UILocalNotification class is available. Remember that it’s available only on iOS 4 and above. The “constructor” intelligently allocates a instance of itself only when it runs on iOS 4. Otherwise the shared instance is always nil.

You should by now appreciate the power of singleton and it’s design elegance. Do note that, because we are going to channel the complete notification mechanism of the application through this single class, we can maintain the application icon’s badge count as a instance variable.

Step 4:

Now, lets write the real meat.

Scheduling a local notification is far easier compared to push notifications. Just allocate a instance of a UILocalNotification and fill in the necessary parameters and submit it to the applications’ shared instance.

In our singleton instance, we will create a helper method that automatically creates a local notification object, fills it with default values properly and schedules it. The helper also takes care of incrementing the badgeCount, thereby maintaining its state.

Step 7:
Write a function within our Singleton to handle the notification payload. In our case, we just NSLog the notification description and reduce the badge count. You can add your app-specific sophisticated logic here.

Now run the app on simulator or device. Note that unlike, push notifications, local notifications work on simulator. Since we schedule a local notification after 20 sec, close the app by clicking the home button on the simulator (or device) and wait. You should see a alert box like this.

Source Code:

The complete source code is available at Github. Feel free to tinker with the code and mainly, if you fork it, and want me to have a look, do drop me a message or leave a comment here.