2014-11-24 79 views
0

我很抱歉這篇較長的文章,但我認爲這是解釋發生什麼事情的唯一方法......我在大量研究之後尋找幫助而沒有答案。 ..我認爲一個大問題...科爾多瓦Jquery移動本地通知onclick變更頁面

因此,這個網絡應用程序安排一個本地通知,基於時間選擇器,用戶與互動.. 我使用科爾多瓦和jquery移動多頁系統...它通過div ID在頁面間切換,導航如下所示:index.html,index.html#page2,index.html#page3 .. 本地通知是cordova Katzer本地插件的java插件。

該插件只能在onDeviceReady函數內部和jQuery Mobile的沒有,像這樣...

document.addEventListener('deviceready', onDeviceReady, false); 

/* JQUERY MOBILE HERE */ 
$(document).on("pagecreate", "#home", function(event) { 
    $("a#btnpage2").click(function (e) { 
    e.stopImmediatePropagation(); 
    e.preventDefault(); 
     $("#page2").pagecontainer("change"); // change to #page2 
    }); 
}); 
$(document).on("pagecreate", "#page2", function(event) { 
    console.log("#page2 created"); 
}); 

function onDeviceReady(){ 
    /* NOTIFICATION PLUGIN HERE */ 
    //create notification 
    var msg = "notification message"; 
    window.plugin.notification.local.add({ 
     id: 'notif', 
     date: dateobject, 
     message: msg, 
     json: JSON.stringify({ test: msg }), 
     title: 'Title', 
     autoCancel: true, 
     ongoing: false 
    }); 
    //onclick event notification 
    window.plugin.notification.local.onclick = function (notif, state, json) { 
     var msg = JSON.parse(json).test; 
    $("#notificationPage").pagecontainer("change", { text: msg}); //pass data and change to #page2 
    } 
    //ontrigger notification 
    window.plugin.notification.local.ontrigger = function (notif, state, json) { 
     var msg = JSON.parse(json).test; 
    } 
} 

當通知被觸發,當我點擊它,它應該改變頁面# notificationPage。 的問題是,該命令裏面的onclick,甚至沒有當我點擊與應用程序運行的通知工作,它拋出這個錯誤:

Uncaugh錯誤:在初始化之前pagecontainer不能調用方法;試圖調用方法'改變'。

但是下面的命令會改變頁面,發現它在google:$ .mobile.changePage(「#notificationPage」)。但只有當應用程序正在運行,而不是中斷。我認爲,如果它在後臺或關閉,即使它沒有中斷,它也不會改變頁面......它會打開由插件定義的活動。當我說在後臺或關閉,並沒有中斷我的意思是應用程序被關閉的主要按鈕,而不是後退按鈕,完全關閉應用程序.. 我想這是處理通知的類:

/* Receiver.class通知插件*/

Intent intent = new Intent(context, ReceiverActivity.class) 
.putExtra(OPTIONS, options.getJSONObject().toString()) 
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
int requestCode = new Random().nextInt(); 
PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
return notification.setContentIntent(contentIntent); 

/* ReceiverActivity.class通知插件*/

Context context  = getApplicationContext(); 
String packageName = context.getPackageName(); 
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName); 
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
launchIntent.putExtra("MSG", msgJson); // here I pass the json message to the new intent, thats gonna open when clicking the notification 
context.startActivity(launchIntent); 

所以基本上,我想單擊通知,然後打開一個特定的頁面,這樣我就可以獲得點擊json值並傳遞給該頁面,然後將其顯示給div元素......看起來我無法使用onDeviceReady之外的通知插件命令,以及onDeviceReady內部的jquery移動命令....除此之外,我必須處理的問題是,如果應用程序關閉並中斷,則執行相同的操作...

At在Java方面,我可以創建另一個活動以及主要的cordova應用程序活動,並在xml中創建一個佈局,並添加一個textview ...在這個新活動的.java文件上,我想我可以將setContentView設置爲此xml佈局,並將textView的文本設置爲我想要的json對象值...此json值與通知的消息相同...我非常確定,95%的人相信這樣做會起作用,而不是teste但是,事情是,它很難維護。

我嘗試的是創建這個新的活動,就像cordova的主要活動,但loadUrl,我設置爲我想去的頁面,而不是LaunchUrl,它從cordova的config.xml加載地址,並將json值作爲url參數添加到意圖創建中,所以在jquery移動端我可以採用document.URL和參數......就像這樣,首先我編輯了Re​​ceiverActivity。從通知插件類:

/* ReceiverActivity.class通知插件*/

Context context  = getApplicationContext(); 
//String packageName = context.getPackageName(); 
Intent launchIntent = new Intent(context, NotificationActivity.class); 
launchIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
launchIntent.putExtra("MSG", msgJson); 
context.startActivity(launchIntent); 

/* NotificationActivity.class科爾多瓦應用第二活性*/

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    String msg; 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    msg = extras.getString("MSG"); 

    String utf_encoded = null; 
    try { 
     utf_encoded = URLEncoder.encode(msg,"UTF-8"); 
    } catch (UnsupportedEncodingException e) {} 

    String url = "file:///android_asset/www/index.html#notificationPage?parameter="+utf_encoded; 
    super.loadUrl(url); 
} 

而上javascript方面,我可以在url中檢索該參數:

document.addEventListener('deviceready', onDeviceReady, false); 

$(document).on("pagebeforechange" , function (event, data) { 
    if (data.toPage[0].id == "notificationPage") { 
     var url = document.URL; 
     var urlParams = decodeURIComponent(url); 
     var onlyParams = urlParams.split('=')[1]; 
     var newchar = " "; 
     var paramValue = onlyParams.split('+').join(newchar); 
     $('#notificationPage #showMessage').empty(); 
     $('#notificationPage #showMessage').append("<p>Message: " + paramValue + "</p>"); 
    } 
}); 
function onDeviceReady(){ 
    /* ondeviceready */ 
} 

這實際上工作,但它有一些錯誤...有時頁面加載,有時頁面不加載,頁面有時會轉向黑屏......它特別是如果應用程序關閉並中斷,但如果它打開,大部分時間會進入黑屏......如果我點擊設備上的後退按鈕,它會「導航回」,但實際上它會進入應該激活的頁面,顯示消息...它像頁面在這個黑色屏幕背後有時它不會出現在前面,除非我使用後退按鈕.. 我超出了選擇...幾乎沒有任何具體和穩定的嘗試解決方案..標誌,JavaScript的,爪哇,在JavaScript重定向url,在Java,似乎沒有工作...

嗯,我不是開發者。我是一名設計師,盡我所能完成這項任務...但上帝,它的努力......理論上,一個簡單的解決方案是將所有內容都保存爲默認設置,當插件「啓動」應用程序或意圖時,或者無論點擊通知,只需使用jquery mobile中的命令運行javascript即可更改頁面......這太棒了! HAHAH 我真的需要幫助..

感謝大家,在讀這...給大家,會盡量幫我... 謝謝大家

回答