2014-04-08 24 views
0

我有一個後臺服務,我應該午餐時立即開始。如果有新的數據可用,此後臺服務會每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」,這樣我就可以知道我點擊的通知和顯示正確的數據。

到目前爲止,我發現的通知是如何午餐的應用程序,並確實工作,但沒有明確的例子如何獲得通知細節。 我跟着這linkdocs,但我使用合金模型。

如果它是有用的,我要添加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你可以克隆它,並嘗試,看看你能不能改變的東西。它不工作呢,我還在努力嘗試解決這個問題。 。 感謝您的任何幫助或反饋!

回答

1

我認爲它的一半。 當掛起意圖創建我們應該加上連活動:

var activity = Ti.Android.currentActivity; 
var pending = Ti.Android.createPendingIntent({ 
    activity:activity, 
    intent : intent 
}); 

和index.js我得到這樣的:

var act = Titanium.Android.currentActivity; 
var _intent = act.intent; 
var message = _intent.getStringExtra("ntfId"); 

這工作只要確定我關閉應用程序,但如果應用程序只是在後臺,我點擊通知它通過null,我不明白爲什麼..

+0

你可以請顯示你的最終代碼,如果你有它的工作。即使我被困在同樣的問題。應用程序打開,但我得到NULL值與getStringExtra函數 – Maverick

+0

我仍然在這工作..我還沒有解決方案 – linnal

0

我已經用Ti.Android.FLAG_ACTIVITY_NEW_TASK | Ti.Android.FLAG_ACTIVITY_MULTIPLE_TASK,它的工作完美。它重新啓動應用程序,但它工作。

並且您忘了將「type:Titanium.Android.PENDING_INTENT_FOR_ACTIVITY」添加到掛起。

+0

你好@ mihai-blaga,我試過了,但它停止在啓動畫面。我創建了一個github項目,如果你有時間可以嘗試它並進行你想要的修改並讓我知道:) [gi​​thub_project](https://github.com/linnal/TiAndroidNotification) – linnal

0

這裏的JS模塊我使用

https://gist.github.com/muka/d4afae20a5732f3937e8

好像正確響應時,應用程序在背景或前景

基本上,主要活動聽newintent事件,並尋找預期的數據在作爲參數收到的意向演員

我很好奇,看看它是否適用於不同的用例,讓我知道。

謝謝