iOS LocalNotifications with multi-context app

You must Login before you can answer or comment on any questions.

Hi,

I've got an app that schedules local notifications from an event callback through an alarm screen using the usual custom events approach. This works well. What doesn't is our notification listener in app.js:

Ti.App.iOS.addEventListener('notification', function(e)
{
    Ti.API.info("local notification received: "+ e.userInfo.alarmId);
}
This doesn't seem to fire. We need it to fire once the app loads/resumes from background so we know which alarm to load. Has anyone any experience of this?

Using a small test app with a single context everything works fine so I'm wondering if they're related. That said I thought app.js context was always loaded. A further point I wondered is if it was because the event callback was on a different thread? Or maybe even the alarm screen itself? Been messing with this for hours and got nowhere!

George

— asked 3 years ago by George Palmer
8 Comments
  • can you provide more code? where is the resume event code, where is the code that you are firing the event?

    yes the doc, might be a little lite...but you haven't provided much here to go on.

    How to ask a question

    — commented 3 years ago by Aaron Saunders

  • As far as I understand it the code that handles resume is unconnected to local notifications but maybe that's what I'm missing. Anyway here's all the related code:

    saveNotification = function(e) {
        alarmId = e.alarmId;
        Ti.App.iOS.scheduleLocalNotification({
            alertBody:'Alarm\n Do something',
            alertAction:"Check",
            sound: "default",
            userInfo:{"alarmId":alarmId},
            date:new Date(new Date().getTime() + 3000),
            repeat:"weekly"
        });
    }
     
    Ti.App.addEventListener('saveNotification', saveNotification);
     
    Ti.App.iOS.addEventListener('notification', function(e){
        Ti.API.info("local notification received: "+ e.userInfo.alarmId);
    }
     
    Ti.App.addEventListener('resume',function(e){
        Ti.API.info("app is resuming from the background");
    });
     
    Ti.App.addEventListener('resumed',function(e){
        Ti.API.info("app has resumed from the background");
    });
     
    Ti.App.addEventListener('pause',function(e){
        Ti.API.info("app was paused from the foreground");
    });
    and then in my other window:
    Ti.App.fireEvent('saveNotification', {'alarmId':123456});
    Note sending the local notification at 3s after the event was just to speed up testing. It doesn't make any difference if it's 5 minutes.

    — commented 3 years ago by George Palmer

  • Just to clarify the other window is a separate context

    — commented 3 years ago by George Palmer

  • Show 5 more comments

2 Answers

what is this?

Ti.App.iOS.addEventListener('notification', function(e)
you probably should not be attaching objects to the Ti.App namespace so I would start with changing that.

— answered 3 years ago by Aaron Saunders
answer permalink
2 Comments
  • That's how the Kitchen Sink does unless I'm missing something?

    — commented 3 years ago by George Palmer

  • my mistake, misread and no way to delete or edit after submit is pressed

    — commented 3 years ago by Aaron Saunders

It appears (at least as of iOS6 and ti mobile 3.0.2) that the Ti.App.fireEvent method blocks while there are any other contexts running inside of application event handlers. As far as I can tell the best way to work around this problem is to do multithreading via web workers running inside a hidden WebView.

Your Answer

Think you can help? Login to answer this question!