I am updating a Titanium mobile application which can schedule alarm notification for events, such that notification will pop up at the scheduled time.
Originally the mobile application is using a titanium module named as benCoding AlarmManager: https://github.com/benbahrenburg/benCoding.AlarmManager to schedule notification. However after I upgraded titanium SDK from 8.3.0 to 9.0.0, I found that there would be error starting the application if I killed the application before the notification scheduled time. For example, if I schedule an alarm notification at 8:00 and the application is not running in the background at 8:00. Although notification will pop up at 8:00, there will be error when starting the application.
E/TiExceptionHandler: (main) [333,333] <embedded>:19295
return originalRequire(moduleId);
^
TypeError: originalRequire is not a function
at global.require (<embedded>:19295:10)
at <embedded>:19555:3
at loadAsync (<embedded>:19481:5)
at _startSnapshot (<embedded>:19552:1)
at /ti.main.js:1:98
at Module._runScript (ti:/module.js:587:9)
at Module.load (ti:/module.js:106:7)
at Function.Module.runModule (ti:/module.js:74:9)
org.appcelerator.kroll.runtime.v8.V8Runtime.nativeRunModule(Native Method)
org.appcelerator.kroll.runtime.v8.V8Runtime.doRunModule(V8Runtime.java:174)
org.appcelerator.titanium.TiApplication.launch(TiApplication.java:791)
org.appcelerator.titanium.TiLaunchActivity.loadScript(TiLaunchActivity.java:98)
org.appcelerator.titanium.TiRootActivity.loadScript(TiRootActivity.java:480)
org.appcelerator.titanium.TiLaunchActivity.onResume(TiLaunchActivity.java:179)
org.appcelerator.titanium.TiRootActivity.onResume(TiRootActivity.java:499)
android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1446)
android.app.Activity.performResume(Activity.java:7939)
android.app.ActivityThread.performResumeActivity(ActivityThread.java:4195)
I included one of my example code on AlarmManager, which will schedule a notification after pressing a button. The notification will be pop up after 2 minutes and pressing the notification will create error message "originalRequire is not a function".
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Ti.UI.setBackgroundColor('#000');
// Add this in so Titanium will add the permission for us.
Titanium.Media.vibrate();
// Add this so Titanium will add the permissions and links needed to play sounds
var sound = Titanium.Media.createSound();
// Bring in the module
var alarmModule = require('bencoding.alarmmanager');
// Create a new instance of the Alarm Manager
var alarmManager = alarmModule.createAlarmManager();
// Create our basic window to put our example buttons on
var win = Titanium.UI.createWindow({
title:'Tab 1', backgroundColor:'#fff', title:'Alarm Manager Tests'
});
var btn1 = Ti.UI.createButton({
title:"Alarm & Notify Basic", top:10, left:5, width: 150, height:75
});
win.add(btn1);
btn1.addEventListener('click', function(e){
scheduleAlert = function(id, title, content) {
id = parseInt(id);
var date = new Date();
alarmManager.cancelAlarmService(id);
// To resolve the problem mutiple alarm at the same time using Android alarmanager
var dateStr = "alarm" + date.getTime();
Ti.API.info("addAlarmService:"+ date.getFullYear() + ", " + date.getMonth() + "," + date.getDate() + "," + date.getHours() + ","+date.getMinutes() + ", "+date.getSeconds());
var hr = date.getHours();
var min = date.getMinutes() + 2;
var sec = date.getSeconds();
alarmManager.addAlarmService({
service : Ti.App.id + '.AlertServiceService',
requestCode : id,
//icon: Ti.Android.R.drawable.star_on, //Optional icon must be a resource id or url
year : date.getFullYear(),
month : date.getMonth(),
day : date.getDate(),
hour : hr,
minute : min,
second : sec
});
var ew = Ti.UI.createAlertDialog({
title : 'Info',
message : "The Service provided will be started at " + hr + ":" + min + ":" + sec,
buttonNames : [Ti.Android.currentActivity.getString(Ti.Android.R.string.ok)]
});
ew.show();
};
scheduleAlert(1, "Testing 123", "");
});
win.open();
I know that benCoding AlarmManager is a bit old and the last updated date is Jan 2018, however I do not know what alternatives can I use to generate notification same as AlarmManager in a Titanium application. Grateful if someone could provide advice for me on the error above or provide some alternatives on AlarmManager. Thanks.