Cordova Local Notification Android Plugin 2.2 upgr

2020-02-29 10:37发布

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 ?

5条回答
等我变得足够好
2楼-- · 2020-02-29 10:57

The whole file including Sana Joseph's comments can be found here:

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

After i had created this, i also found https://github.com/olore/LocalNotifications, which also allows to use cordova pluginstall.

The notification is created and comes up fine using any of these solutions, however - nothing happens when i click on it :( It' neither disappears nor opens the application.

Any clues what i might be missing?

查看更多
Melony?
3楼-- · 2020-02-29 11:02

you must use your callbackContext in order to return additional data to your success/error handlers. the return value must be a boolean.

查看更多
劫难
4楼-- · 2020-02-29 11:06

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.

查看更多
beautiful°
5楼-- · 2020-02-29 11:10

fyi also

change

LocalNotification extends Plugin

for

LocalNotification extends CordovaPlugin

it works for me

查看更多
看我几分像从前
6楼-- · 2020-02-29 11:14

replace it with the "public boolean execute" function and On success add

callbackContext.success();
return true;

and on failure:

return false;
查看更多
登录 后发表回答