Creating simple alarm in Appcelerator Titanium for both Android and iOS

Posted By Ajit Jati | 03-Dec-2014

appcelerator titanium

Well , how it (in-app alarm) came to my mind is a bit neccessary story. A recent titanium application need to set and play ALARMS from within the app by not lettting the app-user know .The concept was simple to call the native alarm-api but in Titanium it was a bit tricky and head full of task to use the local notifications as a complete Alarm.
Technically they are Local notifications which are scheduled to alert us when some particular event oocured via our code in future.

So let me guide you create a instance of simple alarm in iOS and Android.

iOS approach

First we need to register our background service in app.js .This will create the service once the app is based into the background.

// register a background service. this JS will run when the app will go in backgrounded
var bgService = Ti.App.iOS.registerBackgroundService({url:'bg.js'});


Second we create the notification or alarm logic. 

var notification =    Ti.App.iOS.scheduleLocalNotification({
  alertBody:"Your App has gone background",
  userInfo:{"hello":"world","id": 123},
  date:new Date(new Date().getTime() + 3000) // 3 seconds after backgrounding
// Cancel all notifications linked to your app
//Cancel the notification using the id passed in as part of the userInfo object
//Get the notification click event and cancel the particular event
Ti.App.iOS.addEventListener('notification', function(e) {"local notification received: " + JSON.stringify(e));"local notification received with id : " +;

You can explore the advance options and many more about the concept from the official document. And here is a great discussion on the iOS local notifications.

Android approach

For android there is an awesome module which implements the native android's Alarm Manager.

You can explore more about the Native Alram Manager here.

Let me show you some code with comments to use this module.Add this in your app.js.

//Import the module into our Titanium App
var alarmModule = require('bencoding.alarmmanager');
var alarmManager = alarmModule.createAlarmManager();
//Set an Alarm to fire a notification in about two minutes
    icon: Ti.Android.R.drawable.star_on, //Optional icon must be a resource id or url
    minute:2, //Set the number of minutes until the alarm should go off
    contentTitle:'Alarm #1', //Set the title of the Notification that will appear
    contentText:'Alarm & Notify Basic', //Set the body of the notification that will apear
    playSound:true, //On notification play the default sound ( by default off )
    vibrate:true, //On notification vibrate device ( by default off )
    showLights: true, //On notification show the device lights ( by default off )
//Below is an example on how you can provide a full date to schedule your alarm
//Set an Alarm to publish a notification in about two minutes and repeat each minute
    year: now.getFullYear(),
    month: now.getMonth(),
    day: now.getDate(),
    hour: now.getHours(),
    minute: now.getMinutes() + 2, //Set the number of minutes until the alarm should go off
    contentTitle:'Alarm #3', //Set the title of the Notification that will appear
    contentText:'Alarm & Notify Scheduled', //Set the body of the notification that will apear
    repeat:60000 //You can use the words hourly,daily,weekly,monthly,yearly or you can provide milliseconds.
    //Or as shown above you can provide the millesecond value

Don't forget to add recievers in tiapp.xml and inside the <application > tag of your android menifest.

<receiver android:name="bencoding.alarmmanager.AlarmNotificationListener"></receiver>
<receiver android:name="bencoding.alarmmanager.AlarmServiceListener"></receiver>

You can additionally add background services with this module.
Here is the official guidlines.
Hope that helped you. Query me , Cheers !!

Credit :

Request for Proposal

Recaptcha is required.

Sending message..