簡體   English   中英

Cordova Jquery Mobile本地通知onclick更改頁面

[英]Cordova Jquery Mobile Local Notification onclick change page

很長的帖子,我很抱歉,但是我認為這是解釋正在發生的事情的唯一方法。經過大量研究但沒有答案,我正在尋求幫助。我認為這是一個大問題。

因此,此Web應用程序根據用戶與之交互的時間選擇器安排本地通知。.我使用的是cordova和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);

所以基本上,我想單擊通知,然后打開一個特定的頁面,這樣我就可以在click上獲取json值並傳遞到該頁面,然后將其顯示給div元素...似乎我無法使用通知onDeviceReady外部的插件命令,以及onDeviceReady內部的jquery移動命令。...此外,如果應用程序關閉並中斷,我還必須處理同樣的問題...

在Java端,我可以創建一個與主cordova應用程序活動一起的活動,並在xml中創建一個布局,並添加一個textview ...在這個新活動的.java文件中,我想可以將setContentView設置為此xml布局,並將textView的文本設置為我想要的json對象值...此json值與通知消息相同...我很確定,就像95%的人相信這是可行的,尚未經過測試,但問題是,它很難維護。

我試過的是創建此新活動,就像cordova的主要活動一樣,但是將loadUrl設置為我要轉到的頁面,而不是LaunchUrl,它從cordova的config.xml加載地址並傳遞了json我在意圖創建中作為url參數額外添加的值,因此在jquery移動端我可以獲取document.URL和參數...像這樣,首先我從通知插件編輯了Re​​ceiverActivity.class:

/ * 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 cordova應用程序第二個活動* /

@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, Java,在javascript處重定向URL,在Java上,似乎沒有任何作用...

好吧,我不是開發人員。 我是一名設計師,竭盡全力完成此任務……但是,天哪,這很困難……。從理論上講,一個簡單的解決方案是將所有內容保留為默認值,並且當插件“啟動”應用程序或意圖時,或者不管是通過單擊通知,還是使用來自jQuery mobile的命令來運行javascript來更改頁面……那真是太神奇了! 哈哈,我真的需要幫助。

感謝所有正在閱讀此書的人...感謝所有會幫助我的人...謝謝大家

使用此方法:

cordova.plugins.notification.local.on("click", function (notification) {
    alert(notification.text);
}, scope);

這是更新的文檔。

Cordova Jquery Mobile本地通知onclick更改頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM