Titanium - 当用户点击Android Notification时执行操作

时间:2014-08-12 14:24:25

标签: javascript notifications titanium

我有显示Android通知的代码,当有人点击该通知时,如果应用程序在之前的后台,它就会到达前台。但我想在应用程序来到forground后再调用一个javascript函数或运行一个javascript文件。我怎么能这样做?

app.js

if(!Ti.App.Properties.getBool("displayedNotifications")){
    var notifications = ["Apple","Orange","Banana"];

    for(var i = 0; i < notifications.length; i++){
        var intent = Ti.Android.createIntent({
            action: Ti.Android.ACTION_MAIN,
            packageName:"com.company.notificationtest",
            className:"com.company.notificationtest.NotificationtestActivity",
            flags:Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED  | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
        });
        intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
        intent.putExtra("name",notifications[i]);

        Titanium.Android.NotificationManager.notify(i, Titanium.Android.createNotification({
            contentTitle: notifications[i],
            contentText : notifications[i],
            contentIntent: Ti.Android.createPendingIntent({
                intent:intent,
                type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY
            }),
            flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
        }));
    }

    Ti.App.Properties.setBool("displayedNotifications",true);
}

tiapp.xml

<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
    <id>com.company.notificationtest</id>
    <name>NotificationTest</name>
    <version>1.0</version>
    <publisher>not specified</publisher>
    <url></url>
    <description>not specified</description>
    <copyright>not specified</copyright>
    <icon>appicon.png</icon>
    <fullscreen>false</fullscreen>
    <navbar-hidden>false</navbar-hidden>
    <analytics>true</analytics>
    <guid>91e86075-373b-44e0-9416-66183390e8af</guid>
    <property name="ti.ui.defaultunit" type="string">dp</property>
    <android xmlns:android="http://schemas.android.com/apk/res/android">
    </android>    
    <modules>
    </modules>
    <deployment-targets>
        <target device="android">true</target>
    </deployment-targets>
    <sdk-version>3.2.3.GA</sdk-version>
</ti:app>

您还可以在此处找到示例应用形式的代码:https://github.com/VanCoding/TitaniumNotificationTest

1 个答案:

答案 0 :(得分:2)

您需要在Intent对象中设置它,例如:

var intent = Ti.Android.createIntent({
    action: Ti.Android.ACTION_MAIN,
    url: 'activity1.js'
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
Ti.Android.currentActivity.startActivity(intent);

http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Android.Intent

编辑:

好的,这是使用className而不是url的工作应用程序的链接:https://github.com/foolprooflabs/AndroidNotificationsCustomActivity

编辑2:

更新了Github代码,删除了tiapp.xml中不必要的标记