Creating simple alarm in Appcelerator Titanium for both Android

Posted By : Ajit Jati | 09-Mar-2021

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",
  alertAction:"Re-Launch!",
  userInfo:{"hello":"world","id": 123},
  sound:"pop.caf",
  date:new Date(new Date().getTime() + 3000) // 3 seconds after backgrounding
});
// Cancel all notifications linked to your app
Ti.App.iOS.cancelAllLocalNotifications();
//Cancel the notification using the id passed in as part of the userInfo object
Ti.App.iOS.cancelLocalNotification(123);
//Get the notification click event and cancel the particular event
Ti.App.iOS.addEventListener('notification', function(e) {
   Ti.API.info("local notification received: " + JSON.stringify(e));
   Ti.API.info("local notification received with id : " + e.userInfo.id);
   Ti.App.iOS.cancelLocalNotification(e.userInfo.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
alarmManager.addAlarmNotification({
    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
alarmManager.addAlarmNotification({
    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.

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

 

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

 

Credit : http://bencoding.com/2012/06/06/android-alarmmanager-for-titanium/

About Author

Author Image
Ajit Jati

Ajit is an proficient Project Manager specializing in Mobile technologies. He possesses extensive development experience in Titanium, Android, Kotlin, Roku, and PHP. Ajit has successfully delivered various projects in the OTT, AR/VR, and Travel industries. His skill set includes developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within the allocated budget. He excels in collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and effectively manage expectations. He conducts regular project status meetings, ensuring effective communication and providing updates to clients and stakeholders on project progress, risks, and issues. Furthermore, Ajit takes on the role of a coach and mentor for team,offering guidance on project management best practices and assisting in their skill development.

Request for Proposal

Name is required

Comment is required

Sending message..