简体   繁体   English

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

[英]Cordova Jquery Mobile Local Notification onclick change page

I'm sorry for the long post, but I think this is the only way to explain whats happening... I'm looking for help after a lot of research with no answers... A big problem I think... 很长的帖子,我很抱歉,但是我认为这是解释正在发生的事情的唯一方法。经过大量研究但没有答案,我正在寻求帮助。我认为这是一个大问题。

So this web app schedule a local notification based on a time picker that the users interact with.. I'm using cordova and jquery mobile multi-page system... It changes from page to page by div IDs, the navigation looks like this: index.html, index.html#page2, index.html#page3.. The local notification is a java plugin for cordova Katzer Local Plugin. 因此,此Web应用程序根据用户与之交互的时间选择器安排本地通知。.我使用的是cordova和jquery移动多页系统...它随div ID在页面之间变化,导航看起来像这样:index.html,index.html#page2,index.html#page3。。本地通知是Cordova Katzer本地插件的Java插件。

The plugin only works inside the onDeviceReady function and the jquery mobile does not, like this... 插件仅在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;
    }
}

When the notification is fired, and when I click it, it should change the page to #notificationPage. 当通知被触发时,当我单击它时,它应该将页面更改为#notificationPage。 The problem is that the command inside onclick, does not work even when I click the notification with the app running, it throws this error: 问题是,即使我在运行应用程序的情况下单击通知,onclick中的命令也无法使用,它会引发以下错误:

Uncaugh Error: cannot call methods on pagecontainer prior to initialization; Uncaugh错误:初始化前无法调用pagecontainer上的方法; attempted to call method 'change'. 尝试调用方法“更改”。

However the following command DOES change the page, found it on google: $.mobile.changePage( "#notificationPage" ). 但是,以下命令会更改页面,并在google上找到它:$ .mobile.changePage(“ #notificationPage”)。 But only if the app is running and not interrupted. 但仅当应用程序正在运行且未中断时。 I think that if its in background or closed even if its not interrupted, it doesn't change the page... it opens the activity defined by the plugin. 我认为,即使它在后台或关闭,即使它没有被打断,它也不会更改页面...它将打开由插件定义的活动。 When I say in background or closed and not interrupted i mean that app was closed by the main button and not the back button that completely closes the app.. I guess this is the classes that handle the notification: 当我在后台说或被关闭且未中断时,我的意思是该应用是由主按钮关闭的,而不是由完全关闭该应用的后退按钮关闭的。我猜这是处理通知的类:

/* Receiver.class notification plugin */ / * 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 notification plugin */ / * 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);

So basically, I want to click the notification, and open a specific page, so I can get the json value on click and pass to that page and then display it to a div element... It seems I can't use the notification plugin commands outside the onDeviceReady, and neither jquery mobile commands inside the onDeviceReady.... Beside that I have to deal with the problem that is to do the same thing if the app is closed and interrupted... 所以基本上,我想单击通知,然后打开一个特定的页面,这样我就可以在click上获取json值并传递到该页面,然后将其显示给div元素...似乎我无法使用通知onDeviceReady外部的插件命令,以及onDeviceReady内部的jquery移动命令。...此外,如果应用程序关闭并中断,我还必须处理同样的问题...

At the java side, I could create another activity along with the main cordova app activity, and create a layout in xml, and add a textview... On the .java file of this new activity I think I could set the setContentView to this xml layout, and set the text of the textView to the json object value I want... this json value is the same as the message of the notification... I pretty sure, like 95% convinced this would work, not tested yet, but the thing is, its hard to maintenance. 在Java端,我可以创建一个与主cordova应用程序活动一起的活动,并在xml中创建一个布局,并添加一个textview ...在这个新活动的.java文件中,我想可以将setContentView设置为此xml布局,并将textView的文本设置为我想要的json对象值...此json值与通知消息相同...我很确定,就像95%的人相信这是可行的,尚未经过测试,但问题是,它很难维护。

What I tried is create this new activity, exactly like the main activity of cordova, but the loadUrl, I set to the page I want to go, not to LaunchUrl, which loads the address from config.xml of cordova, and passed the json value that I added as extra on the intent creation as a url param so on the jquery mobile side I could take the document.URL and the parameter... like this, first I edited the ReceiverActivity.class from notification plugin: 我试过的是创建此新活动,就像cordova的主要活动一样,但是将loadUrl设置为我要转到的页面,而不是LaunchUrl,它从cordova的config.xml加载地址并传递了json我在意图创建中作为url参数额外添加的值,因此在jquery移动端我可以获取document.URL和参数...像这样,首先我从通知插件编辑了Re​​ceiverActivity.class:

/* ReceiverActivity.class notification plugin */ / * 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 app second activity */ / * 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);
}

And on the javascript side, I could retrive that parameter in the 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 */
}

This actually worked, but it has some bugs... Sometimes the page loads, sometimes the page doesn't load, the page sometimes turned to a black screen... It works specially if the app is closed and interrupted, but if its open, most of the times it goes to a black screen... and if I click the back button on the device, it "navigates back", but it actually goes to the page that should be activated and showing the message... its like the page is behind this black screen sometimes and it won't come out to front unless I use back button.. I'm out of options... tried almost everything with no concrete and stable solution.. Flags, javascript, java, redirect url at javascript, at java, nothing seems to work... 这确实有效,但是存在一些错误...有时页面加载,有时页面没有加载,页面有时变成黑屏...它特别适用于应用程序关闭并中断的情况,但是打开,大多数时候它会进入黑屏...如果我单击设备上的“后退”按钮,它会“向后导航”,但实际上它会转到应激活的页面并显示消息...就像页面有时在黑屏后面,除非我使用后退按钮,否则页面不会出现在前面。.我没有选择...尝试了几乎所有内容,没有具体而稳定的解决方案。.标志,javascript, Java,在javascript处重定向URL,在Java上,似乎没有任何作用...

Well I'm not a developer. 好吧,我不是开发人员。 I'm a designer, putting all my efforts to finish this... but god, its hard.... Theoretically a easy solution would be leaving everything at defaults, and when the plugin "launch" the app or the intent, or whatever by clicking the notification, just run a javascript with the command from jquery mobile that change pages... That would be amazing! 我是一名设计师,竭尽全力完成此任务……但是,天哪,这很困难……。从理论上讲,一个简单的解决方案是将所有内容保留为默认值,并且当插件“启动”应用程序或意图时,或者不管是通过单击通知,还是使用来自jQuery mobile的命令来运行javascript来更改页面……那真是太神奇了! hahah I really need help.. 哈哈,我真的需要帮助。

Thanks to everyone that is reading this... To everyone that will try to help me... Thank you all 感谢所有正在阅读此书的人...感谢所有会帮助我的人...谢谢大家

Use this method: 使用此方法:

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

Here is the updated doc. 这是更新的文档。

Cordova Jquery Mobile Local Notification onclick change page Cordova Jquery Mobile本地通知onclick更改页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM