繁体   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