Pregunta

hi I am using phonegap local notification plugin in my android project. notification are working great. i can delete a notification with ID but in my app i have to delete all notification option, for which this plugin gives a method to cancelAll notification. which is:-

    plugins.localNotification.cancelAll();

but this is not working. i am using this plugin from here https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification any help will be appreciated.

¿Fue útil?

Solución

I think this might be the answer to this one. As it says in the description of the cancelAllNotifications() method (LocalNotification.java line 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.

However, take a look at the code where the cancelAllNotifications() method is called (line 59):

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

Here's what unpersistAlarmAll() looks like (line 181):

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

alarmSettingsEditor.clear();

return alarmSettingsEditor.commit();
}

What's happening is that the alarmIds are being cleared from sharedPreferences before cancelAllNotifications() is called, effectively leaving no alarms to be canceled.

I'm not sure where the best place to move the call to unpersistAlarmAll() is, and I'd be happy to get some suggestions. In the meantime I have moved it to within cancelAllNotifications(), after the result returned from alarm.cancelAll(alarmSettings) is tested (line 133), like so:

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

So far this seems to be working fine. Perhaps another way to do this would be to scrap the whole unpersistAlarmAll() method and instead call unpersistAlarm(String alarmId) (line 168) after each individual alarm is successfully canceled.

Otros consejos

Depending on what you are trying to do, you could use the "Statusbar notificaiton". This plugin will enable you to put message on the android status bar.

https://github.com/phonegap/phonegap-plugins/tree/master/Android/StatusBarNotification

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top