hi我在我的android项目中使用phonegap本地通知插件。通知效果很好。我可以删除ID为的通知但在我的应用程序中,我必须删除所有通知选项,为此插件提供了一个取消所有通知的方法。即:-
plugins.localNotification.cancelAll();
但这不起作用。我在这里使用这个插件https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification任何帮助都将不胜感激。
我认为这可能是这个问题的答案。正如它在cancelAllNotifications()
方法的描述中所说(LocalNotification.java第124行):
Android只能注销特定的警报。没有cancelAll这样的东西。因此,我们依赖共享首选项,它保存我们所有的警报,以循环浏览这些警报并逐一注销它们。
但是,看看调用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()
之前,将从sharedPreferences中清除alarmId,实际上不会取消任何警报。
我不确定将呼叫转移到unpersistAlarmAll()
的最佳位置,我很乐意得到一些建议。同时,在测试了alarm.cancelAll(alarmSettings)
返回的结果(第133行)之后,我将其移到了cancelAllNotifications()
中,如下所示:
if (result) {
unpersistAlarmAll();
return new PluginResult(PluginResult.Status.OK);
到目前为止,这似乎运行良好。也许另一种方法是放弃整个unpersistAlarmAll()
方法,而是在每个单独的警报被成功取消后调用unpersistAlarm(String alarmId)
(第168行)。
根据您要做的操作,您可以使用"Statusbar通知"。这个插件将使您能够将消息放在android状态栏上。
https://github.com/phonegap/phonegap-plugins/tree/master/Android/StatusBarNotification