首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手柄LocalNotification点击

手柄LocalNotification点击
EN

Stack Overflow用户
提问于 2014-12-12 10:37:59
回答 1查看 942关注 0票数 0

在我的钛iOS应用程序中,我使用以下代码显示一个LocalMessage:

代码语言:javascript
复制
Ti.App.iOS.scheduleLocalNotification({
    alertBody:"New Notification!",
    userInfo: {id:1234}
});

但是,现在我无法了解如何在用户点击此通知时执行代码。我试过以下几种方法:

代码语言:javascript
复制
Ti.App.iOS.addEventListener("localnotificationaction",function(){
    //my code
});

但是代码不会以这种方式执行。我也试过监听“通知”-event,但没有成功。

这是如何正确完成的呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-12 11:22:23

我认为你应该用:

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

如docs中所指定的:http://docs.appcelerator.com/titanium/3.0/#!/api/Titanium.App.iOS-event-notification

看看这里的完整例子:https://jira.appcelerator.org/browse/TIMOB-18182 Maby你错过了一个步骤吗?

创建一个新项目并尝试如下:

index.js:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
    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();
    });

使用此应用程序在日志上输出:

代码语言:javascript
复制
[INFO] :   registered background service = [object TiAppiOSBackgroundService]
[INFO] :   app was paused from the foreground
[INFO] :   Hello from Running BG service - bg.js

-在我点击横幅之后

代码语言:javascript
复制
[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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27441787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档