gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:54:04 25 4
gpt4 key购买 nike

您好,我在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com