我有一个后台服务,我应该午餐时立即开始。如果有新的数据可用,此后台服务会每5秒钟向服务器询问一次有新数据,我会创建通知。当我点击通知应用程序时,但我无法检索我在创建通知时添加的意图附加参数。Titanium Android通过通知检索意图演员
代码如下: index.js
startService();
function startService(){
if(OS_ANDROID){
log("START SERVICE");
var intent = Titanium.Android.createServiceIntent({
url: 'service.js'
});
Titanium.Android.startService(intent);
}
}
//===========================================
// I tried to add this code so that I could get the intent but it does not work
if(OS_ANDROID){
log("==========================" + JSON.stringify(Ti.Android.currentActivity));
// prints : {"bubbleParent":true,"actionBar":{"navigationMode":0,"bubbleParent":true,"title":null,"apiName":"Ti.Android.ActionBar"},"apiName":"Ti.Android.Activity"}
var _intent = Ti.Android.currentActivity.getIntent();
log(JSON.stringify(_intent));
//prints {"bubbleParent":true,"action":null,"type":null,"data":null,"flags":268435456,"apiName":"Ti.Android.Intent"}
if (_intent.hasExtra('ntfId')) {
log("========= INTENT HAS EXTRA =========");
}else{
log("========= NO INTENT HAS EXTRA =========");
}
var currActivity = Titanium.Android.currentActivity;
var passedInText = currActivity.getIntent().getStringExtra(Ti.Android.EXTRA_TEXT);
log(passedInText);
//prints null
}
service.js
function createNotification(data){
log(data.id + " <<<<<<>>>>>> " + data.type);
var activity = Ti.Android.currentActivity;
var intent = Ti.Android.createIntent({
className : 'packageName.AppNameActivity',
action: Ti.Android.ACTION_MAIN,
packageName: Ti.App.id,
flags: Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_SINGLE_TOP
});
intent.addCategory(Titanium.Android.CATEGORY_LAUNCHER);
intent.putExtra("ntfId", data.id);
var pending = Ti.Android.createPendingIntent({
intent : intent
});
var notification = Ti.Android.createNotification({
contentIntent : pending,
contentTitle : data.title,
contentText : data.desription,
tickerText : "AppName",
icon : Ti.App.Android.R.drawable.appicon,
sound: 'default',
defaults: Titanium.Android.NotificationManager.DEFAULT_VIBRATE,
flags : Titanium.Android.ACTION_DEFAULT | Titanium.Android.FLAG_AUTO_CANCEL | Titanium.Android.FLAG_SHOW_LIGHTS
});
Ti.Android.NotificationManager.notify(data.notification_id, notification);
}
我想我的应用程序打开时得到 “ntfId”,这样我就可以知道我点击的通知和显示正确的数据。
到目前为止,我发现的通知是如何午餐的应用程序,并确实工作,但没有明确的例子如何获得通知细节。 我跟着这link和docs,但我使用合金模型。
如果它是有用的,我要添加TiApp.xml太:
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="android.permission.VIBRATE"/>
<application android:debuggable="true">
<activity
android:configChanges="keyboardHidden|orientation"
android:name="org.appcelerator.titanium.TiActivity"
android:screenOrientation="portrait"/>
<activity
android:configChanges="keyboardHidden|orientation"
android:label="Appname"
android:launchMode="singleTask"
android:name=".AppnameActivity"
android:screenOrientation="portrait" android:theme="@style/Theme.Titanium">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<services>
<service url="service.js"/>
</services>
</android>
我如何从通知的意图通过了额外的参数?似乎当我做getCurrentActivity.getIntent()这是另一种意图完全不同于我想要的。
你能告诉我一些事情或指向的地方,也许我错过了看? 谢谢!
最后编辑 我被推gihub我的项目,TiAndroidNotification你可以克隆它,并尝试,看看你能不能改变的东西。它不工作呢,我还在努力尝试解决这个问题。 。 感谢您的任何帮助或反馈!
你可以请显示你的最终代码,如果你有它的工作。即使我被困在同样的问题。应用程序打开,但我得到NULL值与getStringExtra函数 – Maverick
我仍然在这工作..我还没有解决方案 – linnal