【发布时间】:2026-01-30 11:30:02
【问题描述】:
我很抱歉发了这么长的帖子,但我认为这是解释正在发生的事情的唯一方法......经过大量没有答案的研究后,我正在寻求帮助......我认为这是一个大问题。 ..
因此,此 Web 应用程序会根据用户与之交互的时间选择器来安排本地通知。 我正在使用cordova和jquery移动多页系统...它通过div ID逐页更改,导航如下所示:index.html,index.html#page2,index.html#page3.. 本地通知是 cordova Katzer Local Plugin 的 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里面的命令,即使我在应用程序运行的情况下点击通知,它也不起作用,它会抛出这个错误:
未捕获错误:无法在初始化之前调用 pagecontainer 上的方法;试图调用方法'change'。
但是,以下命令确实更改了页面,在 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 mobile 命令。除此之外,如果应用程序关闭和中断,我必须处理同样的事情......
在 java 方面,我可以创建另一个活动以及主要的 Cordova 应用程序活动,并在 xml 中创建一个布局,并添加一个 textview...在这个新活动的 .java 文件上,我想我可以设置setContentView 到这个 xml 布局,并将 textView 的文本设置为我想要的 json 对象值......这个 json 值与通知的消息相同......我很确定,就像 95% 相信这会起作用,尚未测试,但问题是,它很难维护。
我尝试的是创建这个新活动,就像cordova的主要活动一样,但是loadUrl,我设置为我想去的页面,而不是LaunchUrl,它从cordova的config.xml加载地址,并且传递了我在意图创建中作为 url 参数额外添加的 json 值,因此在 jquery 移动端我可以获取 document.URL 和参数......就像这样,首先我从通知插件编辑了 ReceiverActivity.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 即可更改页面......那将是惊人的!哈哈 我真的需要帮助..
感谢所有阅读本文的人...感谢所有愿意帮助我的人... 谢谢大家
【问题讨论】:
标签: java javascript android jquery cordova