0

In my Titanium iOS app, I'm displaying a LocalMessage using this code:

Ti.App.iOS.scheduleLocalNotification({
    alertBody:"New Notification!",
    userInfo: {id:1234}
});

But now I can't find out how to execute code when the user taps on this notification. I've tried the following:

Ti.App.iOS.addEventListener("localnotificationaction",function(){
    //my code
});

But the code does not get executed this way. I've also tried listening on the "notification"-event but with no luck.

How is this done correctly?

Van Coding
  • 24,244
  • 24
  • 88
  • 132

1 Answers1

0

I think you should use:

// listen for a local notification event
Ti.App.iOS.addEventListener('notification',function(e)
{
    Ti.API.info("local notification received: "+JSON.stringify(e));
});

as specified in the docs: http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.App.iOS-event-notification

Check out full example here: https://jira.appcelerator.org/browse/TIMOB-18182 Maby you missed a step ?

create a new project and try this:

index.js:

    var win = Ti.UI.createWindow({
        backgroundColor:'white'
    });
    win.open();
    var label = Ti.UI.createLabel({
        top: 20,
        height: 200,
        width: 200,
        text: "Background the app"
    });
    win.add(label);

    function isiOS4Plus()
    {
        // add iphone specific tests
        if (Titanium.Platform.name == 'iPhone OS')
        {
            var version = Titanium.Platform.version.split(".");
            var major = parseInt(version[0],10);

            // can only test this support on a 3.2+ device
            if (major >= 4)
            {
                return true;
            }
        }
        return false;
    }

    if (isiOS4Plus())
    {
        // register a background service. this JS will run when the app is backgrounded but screen is OFF!!!
        var service = Ti.App.iOS.registerBackgroundService({url:'bg.js'});

        Ti.API.info("registered background service = "+service);

        // listen for a local notification event
        Ti.App.iOS.addEventListener('notification',function(e)
        {
            Ti.API.info("local notification received: "+JSON.stringify(e));
        });

        // fired when an app resumes for suspension
        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");
        });

        //This event determines that the app it was just paused
        Ti.App.addEventListener('pause',function(e){
            Ti.API.info("app was paused from the foreground");
        });
    }

bg.js:

    var value = "Hello from Running BG service - bg.js";
    Ti.API.info(value);

    var notification = Ti.App.iOS.scheduleLocalNotification({
        alertBody:"App was put in background",
        alertAction:"Re-Launch!",
        userInfo:{"hello":"world"},
        date:new Date(new Date().getTime() + 3000) // 3 seconds after backgrounding
    });

    Ti.App.iOS.addEventListener('notification',function(){
        Ti.API.info('background event received = '+notification);
        //Ti.App.currentService.unregister();
    });

Output on the log with this app:

[INFO] :   registered background service = [object TiAppiOSBackgroundService]
[INFO] :   app was paused from the foreground
[INFO] :   Hello from Running BG service - bg.js

------ AFTER I CLICK THE BANNER-------------

[INFO] :   app is resuming from the background
[INFO] :   local notification received: {"sound":null,"alertBody":"App was put in background","alertAction":"Re-Launch!","date":"2014-12-12T12:52:34.743Z","alertLaunchImage":null,"timezone":"America/Los_Angeles","badge":0,"userInfo":{"hello":"world"},"bubbles":true,"type":"notification","source":{},"cancelBubble":false}
[INFO] :   background event received = [object TiAppiOSLocalNotification]
[INFO] :   app has resumed from the background
Jeroen
  • 1,991
  • 2
  • 16
  • 32
  • The problem with this is that the event also gets called when the notifications gets displayed. Isn't there an event that only fires when the badge gets tapped and not displayed? – Van Coding Dec 12 '14 at 12:49