android - phonegap android 本地通知 cancelAll 不工作

标签 android cordova localnotification

您好,我在我的 android 项目中使用了 phonegap 本地通知插件。 通知效果很好。 我可以删除带有 ID 的通知 但在我的应用程序中,我必须删除所有通知选项,为此插件提供了取消所有通知的方法。 这是:-

    plugins.localNotification.cancelAll();

但这行不通。 我在这里使用这个插件 https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification 任何帮助将不胜感激。

最佳答案

我认为这可能是这个问题的答案。正如它在 cancelAllNotifications() 方法(LocalNotification.java 第 124 行)的描述中所说:

Android can only unregister a specific alarm. There is no such thing as cancelAll. Therefore we rely on the Shared Preferences which holds all our alarms to loop through these alarms and unregister them one by one.

但是,请看一下调用 cancelAllNotifications() 方法的代码(第 59 行):

} else if (action.equalsIgnoreCase("cancelall")) {
    unpersistAlarmAll();
    return this.cancelAllNotifications();
}

这是 unpersistAlarmAll() 的样子(第 181 行):

private boolean unpersistAlarmAll() {
final Editor alarmSettingsEditor = this.cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();

alarmSettingsEditor.clear();

return alarmSettingsEditor.commit();
}

正在发生的事情是,在 cancelAllNotifications() 被调用之前,alarmIds 正在从 sharedPreferences 中清除,实际上没有要取消的警报。

我不确定将调用移至 unpersistAlarmAll() 的最佳位置在哪里,我很乐意得到一些建议。同时,在测试从 alarm.cancelAll(alarmSettings) 返回的结果(第 133 行)后,我将其移至 cancelAllNotifications() 内,如下所示:

if (result) {
    unpersistAlarmAll();
    return new PluginResult(PluginResult.Status.OK);

到目前为止,这似乎工作正常。也许另一种方法是废弃整个 unpersistAlarmAll() 方法,而是在成功取消每个单独的警报后调用 unpersistAlarm(String alarmId)(第 168 行)。

关于android - phonegap android 本地通知 cancelAll 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13697015/

相关文章:

android - Flutter中类似android的JSON转POJO(Object)

javascript - 在javascript中动态创建数组

ios - 关闭应用程序时设置警报

javascript - JS不识别cordova的插件

iphone - 本地通知不会消失

android - Android 中用于蓝牙的 API 查找我个人资料中的即时警报服务

android - 当您使用距离过滤器时,gps 如何知道它移动了 x 米?

具有内部间距的 Android NavigationView

api - 移动 HTML5 应用程序如何使用 OAuth 2 协议(protocol)?

javascript - 没有 phonegap.js