Cordova Local Notification Android Plugin 2.2 upgrade
android, cordova, cordova-2.0.0, localnotification, phonegap-plugins
Solution
Ok the local notifications plugin is finally working with cordova 2.2 :) Now here are the modifications needed:
1) Replace
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
with
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
2) Replace
public PluginResult execute(String action, JSONArray optionsArr, String callBackId)
with
public pluginresult execute(String action, JSONArray args, CallbackContext callbackContext)
3) Add
callbackContext.success();
return true;
or
return false;
as the return type of the function.
4) Replace
this.ctx
with
cordova.getActivity()
5)Add
import yourapplication.name.R;
To AlarmReciever.Java
That's it :) Hope it helps.
Problem
I'm working on a "Reminders" application on Android using Phonegap[Cordova 2.2]. The user enters a specific date for his reminder and I'm supposed to notify him on time. I use Android's Notification Plugin but it supports earlier versions of phone gap. I followed this tutorial to resolve conflicts between cordova 2.2 and previous ones, now a lot of issues have been fixed, but I still can't fix some: ``` public PluginResult execute(String action, JSONArray optionsArr, String callBackId) { alarm = new AlarmHelper(cordova.getActivity()); Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action); PluginResult result = null; final AlarmOptions alarmOptions = new AlarmOptions(); alarmOptions.parseOptions(optionsArr); ``` This function has a problem with this line: ``` public PluginResult execute(String action, JSONArray optionsArr, String callBackId) ``` and when I replace it with this line: ``` public boolean execute(String action, JSONArray optionsArr, CallbackContext callbackContext) { ``` The error is fixed, but another error shows at this function: ``` persistAlarm(alarmId, optionsArr); return this.add(daily, title, subTitle, ticker, alarmId, alarmOptions.getCal()); } else if (action.equalsIgnoreCase("cancel")) { unpersistAlarm(alarmId); return this.cancelNotification(alarmId); } else if (action.equalsIgnoreCase("cancelall")) { unpersistAlarmAll(); return this.cancelAllNotifications(); } return result; } ``` That the return type cannot be converted to boolean, So how can I fix it ? Update: I replaced the return type to be boolean and that's how it is now: ``` @Override public boolean execute(String action, JSONArray optionsArr, CallbackContext callBackId) { Log.d(PLUGIN_NAME, "optionsArr: " + optionsArr.toString()); alarm = new AlarmHelper(cordova.getActivity()); Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action); //PluginResult result = null; boolean result = true; final AlarmOptions alarmOptions = new AlarmOptions(); alarmOptions.parseOptions(optionsArr); /* * Determine which action of the plugin needs to be invoked */ String alarmId = alarmOptions.getNotificationId(); if (action.equalsIgnoreCase("add")) { final boolean daily = alarmOptions.isRepeatDaily(); final String title = alarmOptions.getAlarmTitle(); final String subTitle = alarmOptions.getAlarmSubTitle(); final String ticker = alarmOptions.getAlarmTicker(); persistAlarm(alarmId, optionsArr); this.add(daily, title, subTitle, ticker, alarmId, alarmOptions.getCal()); callBackId.success(); return true; } else if (action.equalsIgnoreCase("cancel")) { unpersistAlarm(alarmId); this.cancelNotification(alarmId); callBackId.success(); return true; } else if (action.equalsIgnoreCase("cancelall")) { unpersistAlarmAll(); this.cancelAllNotifications(); callBackId.success(); return true; } return result; } ``` Now , it's working, but when I click on the notification, the application doesn't open and the notification isn't gone ... how can I fix this ?