Katzer 本地通知不起作用 - Cordova

Posted

技术标签:

【中文标题】Katzer 本地通知不起作用 - Cordova【英文标题】:Katzer local notification not working - Cordova 【发布时间】:2015-03-24 08:40:18 【问题描述】:

我说的是这个插件katzer/cordova-plugin-local-notifications

几个月前我用过它。现在我发现它在很多方面都发生了变化。所以我尝试创建一个简单的通知。

我安装了插件

cordova plugins add https://github.com/katzer/cordova-plugin-local-notifications

接下来我尝试了一些简单的方法

alert("enter");    

var now             = new Date().getTime(),
_5_sec_from_now = new Date(now + 5*1000);

cordova.plugins.notification.local.schedule(
    title: "Production Jour fixe",
    text: "Duration 1h",
    at: _5_sec_from_now,

);

alert("exit");  

什么也没发生,没有错误显示。两个警报都完美显示。

然后我尝试了一些更简单的方法。

cordova.plugins.notification.local.schedule( message:"Hello World" );

cordova.plugins.notification.local.get(0, function (notification) 
    alert(notification.text); //=> "Hello World"
    alert(notification.message); //=> undefined
);

再次没有。 Eclipse logcat 正在显示这个

03-24 08:25:51.922: E/PluginManager(2422): Uncaught exception from plugin
03-24 08:25:51.922: E/PluginManager(2422): java.lang.NullPointerException
03-24 08:25:51.922: E/PluginManager(2422):  at de.appplant.cordova.plugin.localnotification.LocalNotification.execute(LocalNotification.java:133)
03-24 08:25:51.922: E/PluginManager(2422):  at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:65)
03-24 08:25:51.922: E/PluginManager(2422):  at org.apache.cordova.PluginManager.execHelper(PluginManager.java:242)
03-24 08:25:51.922: E/PluginManager(2422):  at org.apache.cordova.PluginManager.exec(PluginManager.java:227)
03-24 08:25:51.922: E/PluginManager(2422):  at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:53)
03-24 08:25:51.922: E/PluginManager(2422):  at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
03-24 08:25:51.922: E/PluginManager(2422):  at android.webkit.JWebCoreJavaBridge.sharedTimerFired(Native Method)
03-24 08:25:51.922: E/PluginManager(2422):  at android.webkit.JWebCoreJavaBridge.fireSharedTimer(JWebCoreJavaBridge.java:92)
03-24 08:25:51.922: E/PluginManager(2422):  at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:108)
03-24 08:25:51.922: E/PluginManager(2422):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 08:25:51.922: E/PluginManager(2422):  at android.os.Looper.loop(Looper.java:137)
03-24 08:25:51.922: E/PluginManager(2422):  at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
03-24 08:25:51.922: E/PluginManager(2422):  at java.lang.Thread.run(Thread.java:856)
03-24 08:25:53.890: W/EGL_genymotion(2422): eglSurfaceAttrib not implemented
03-24 08:25:55.506: E/PluginManager(2422): Uncaught exception from plugin
03-24 08:25:55.506: E/PluginManager(2422): java.lang.NullPointerException
03-24 08:25:55.506: E/PluginManager(2422):  at de.appplant.cordova.plugin.localnotification.LocalNotification.execute(LocalNotification.java:133)
03-24 08:25:55.506: E/PluginManager(2422):  at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:65)
03-24 08:25:55.506: E/PluginManager(2422):  at org.apache.cordova.PluginManager.execHelper(PluginManager.java:242)
03-24 08:25:55.506: E/PluginManager(2422):  at org.apache.cordova.PluginManager.exec(PluginManager.java:227)
03-24 08:25:55.506: E/PluginManager(2422):  at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:53)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewCore.access$6800(WebViewCore.java:57)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1788)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)
03-24 08:25:55.506: E/PluginManager(2422):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 08:25:55.506: E/PluginManager(2422):  at android.os.Looper.loop(Looper.java:137)
03-24 08:25:55.506: E/PluginManager(2422):  at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
03-24 08:25:55.506: E/PluginManager(2422):  at java.lang.Thread.run(Thread.java:856)
03-24 08:25:55.510: E/PluginManager(2422): Uncaught exception from plugin
03-24 08:25:55.510: E/PluginManager(2422): java.lang.NullPointerException
03-24 08:25:55.510: E/PluginManager(2422):  at de.appplant.cordova.plugin.localnotification.LocalNotification.execute(LocalNotification.java:133)
03-24 08:25:55.510: E/PluginManager(2422):  at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:65)
03-24 08:25:55.510: E/PluginManager(2422):  at org.apache.cordova.PluginManager.execHelper(PluginManager.java:242)
03-24 08:25:55.510: E/PluginManager(2422):  at org.apache.cordova.PluginManager.exec(PluginManager.java:227)
03-24 08:25:55.510: E/PluginManager(2422):  at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:53)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewCore.nativeMouseClick(Native Method)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewCore.access$6800(WebViewCore.java:57)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewCore$EventHub.dispatchWebKitEvent(WebViewCore.java:1788)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvent(WebViewInputDispatcher.java:689)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.dispatchWebKitEvents(WebViewInputDispatcher.java:639)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher.access$800(WebViewInputDispatcher.java:78)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewInputDispatcher$WebKitHandler.handleMessage(WebViewInputDispatcher.java:1153)
03-24 08:25:55.510: E/PluginManager(2422):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 08:25:55.510: E/PluginManager(2422):  at android.os.Looper.loop(Looper.java:137)
03-24 08:25:55.510: E/PluginManager(2422):  at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:812)
03-24 08:25:55.510: E/PluginManager(2422):  at java.lang.Thread.run(Thread.java:856)

我在 Genymotion 模拟器 4.2.2 设备映像和 Xperia - android 4.4.4 中进行了测试。我一直在等待 deviceready 事件触发。 js中没有其他错误。谁能告诉我我错过了什么?有什么问题吗?

【问题讨论】:

【参考方案1】:

没有找到问题的原因,但这里是解决方案。

这是一个较旧的项目,目标sdk是16,所以我将其更新为19。

重新安装所有插件。它现在正在工作。我猜这是某种依赖/版本错误。使用新的插件版本,它可以按预期工作。

【讨论】:

【参考方案2】:

为了我的项目(因为它是一个遗留项目,尽管它指向 sdk 19)我不得不使用 0.7.8 版本,并且 nullPointerException 消失了,一切都开始完美运行。

程序

    先删除当前最新版本的插件。

    $ phonegap plugin rm de.appplant.cordova.plugin.local-notification
    

    然后安装0.7.8版

    $ phonegap plugin add de.appplant.cordova.plugin.local-notification@0.7.8
    

然后跟着docs for that version

疑难解答

如果您对phonegap plugin rm 有问题,您可以尝试重新安装最新版本并卸载以正确安装 0.7.8。

为了成功安装 0.7.8 版,我必须从 /src 中删除一些 de.applant.cordova.plugin.localnotification 包,以及从 /assets/www/plugins 中删除 js 文件夹 de.appplant.cordova.plugin.local-notification

【讨论】:

以上是关于Katzer 本地通知不起作用 - Cordova的主要内容,如果未能解决你的问题,请参考以下文章

Katzer 电子邮件插件在 Cordova 中不起作用 - IOS

Ionic 1 本地通知在 Ionic View 应用程序中不起作用

Cordova katzer 插件后台模式在 iOS-9 上不起作用

在 IBM Worklight 中使用 katzer 本地通知

科尔多瓦闹钟

在 PhoneGap/Cordova 中安排本地通知在 iOS 模拟器上不起作用