Hello,
I hope someone can help me out.
I’ve been working on an app where users add in the settings menu (in 24-hour format) the time when they wish to be alerted for some thing or another – they have up to 10 slots. I want them to get the alert and alarm when they are NOT in the app; but I can’t get it to work properly. Most times it does not work, unless intermittently when they manually resync their device, and even then they get alerted at the wrong time.
My code is as follows:-
currentTime = current time
nextTime = alarm time set by user
nextEvent = name of next event
promptMessageNotification = the alarm message
AppView.mc:
// Send notification message to background event to trigger alarms
if (nextTime != currentTime) {
promptMessageNotification = "";
} else {
promptMessageNotification = "ALARM!!\n”+nextEvent;
}
Storage.setValue("PromptMessageNotification", promptMessageNotification);
// background event to notify user when event is due, outside of the app
function setBackgroundEvent() {
System.println("Temporal Event set");
if (nextTime != currentTime) {
} else {
Background.registerForTemporalEvent(nextTimeAlarm);
}
}
AppApp.mc:
(:background)
class AppApp extends Application.AppBase {
var notificationView;
// onStop() is called when your application is exiting
function onStop(state) {
if (notificationView) {
notificationView.setBackgroundEvent();
}
}
// Return the initial view of your application here
function getInitialView() {
notificationView = new AppView();
return [ notificationView, new AppDelegate()];
}
function getServiceDelegate() {
return [new BackgroundService()];
}
}
BackgroundService.mc:
(:background)
class BackgroundService extends System.ServiceDelegate {
var promptMessageNotification1 = Storage.getValue("PromptMessageNotification") == null ? "" : Storage.getValue("PromptMessageNotification");
function initialize() {
System.ServiceDelegate.initialize();
System.println(promptMessageNotification1);
}
function onTemporalEvent() {
if (promptMessageNotification1.equals("")) {
Background.exit(null);
} else {
Background.requestApplicationWake(promptMessageNotification1);
Background.exit(null);
}
}
}
I would really appreciate some help with this. Thanks in advance.