Titanium - Perform Action when user clicks on Android Notification

百般思念 提交于 2019-12-14 02:09:16

问题


I have code that displays an android notification and when someone clicks on that notification, the app comes to the foreground, if it was in the background before. But I want to additionally call a javascript function or run a javascript file after the app came to the forground. How can I do that?

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>

You can also find the code in form of an example app here: https://github.com/VanCoding/TitaniumNotificationTest


回答1:


You need to set that in your Intent object e.g.:

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

EDIT:

OK here's a link to a working application using className instead of url: https://github.com/foolprooflabs/AndroidNotificationsCustomActivity

EDIT 2:

Updated Github code, removed unnecessary tag in tiapp.xml



来源:https://stackoverflow.com/questions/25266834/titanium-perform-action-when-user-clicks-on-android-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!