问题描述
我的代码显示了一个 android 通知,当有人点击该通知时,应用程序会进入前台,如果它之前在后台.但我想在应用程序上线后另外调用一个 javascript 函数或运行一个 javascript 文件.我该怎么做?
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>
您还可以在此处找到示例应用形式的代码:https://github.com/VanCoding/TitaniumNotificationTest
You can also find the code in form of an example app here: https://github.com/VanCoding/TitaniumNotificationTest
推荐答案
您需要在您的 Intent 对象中进行设置,例如:
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
好的,这里有一个使用 className 而不是 url 的工作应用程序的链接:https://github.com/foolprooflabs/AndroidNotificationsCustomActivity
OK here's a link to a working application using className instead of url: https://github.com/foolprooflabs/AndroidNotificationsCustomActivity
编辑 2:
更新 Github 代码,移除 tiapp.xml 中不必要的标签
Updated Github code, removed unnecessary tag in tiapp.xml
这篇关于Titanium - 当用户点击 Android 通知时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!