We have implemented push notification with cordova-plugin-marketingcloudsdk few years back in our Cordova app (as per package.json the version it shows 7.1.0)
In IOS, push notification is working fine. but in android it stopped working 8 - 9 months back.
These are our app configurations
package.json
cordova": "^9.0.0"
Config.xml
<engine name="android" spec="~6.3.0" />
<preference name="android-minSdkVersion" value="23" />
<preference name="android-targetSdkVersion" value="28" />
As per our analysis by adding some logs in MCCordovaPlugin, it shows like,MarketingCloudSdk#init has not been called
. PFB the detailed log
V/~!MCCordova: >>>>>> initialize cordova = org.apache.cordova.CordovaActivity$1@af17ff4 V/~!MCCordova: >> Inside execute >> action = registerEventsChannel V/~!MCCordova: >> handleStaticAction >> switch >> action = registerEventsChannel V/~!MCCordova: >> registerEventsChannel V/~!MCCordova: >> Inside execute >> action = isPushEnabled V/~!MCCordova: >> handleStaticAction >> switch >> action = isPushEnabled V/~!MCCordova: >> getActionHandler >> switch >> action = isPushEnabled V/~!MCCordova: >> Inside isPushEnabled V/~!MCCordova: >> MarketingCloudSdk#init has not been called V/~!MCCordova: >> Inside execute >> action = getDeviceId V/~!MCCordova: >> handleStaticAction >> switch >> action = getDeviceId V/~!MCCordova: >> getActionHandler >> switch >> action = getDeviceId V/~!MCCordova: >> Inside getDeviceID V/~!MCCordova: >> MarketingCloudSdk#init has not been called
Update
public class MCCordovaPlugin extends CordovaPlugin {
static final String TAG = "~!MCCordova";
private CallbackContext eventsChannel = null;
private PluginResult cachedNotificationOpenedResult = null;
private boolean notificationOpenedSubscribed = false;
private static JSONObject fromMap(Map<String, String> map) throws JSONException {
JSONObject data = new JSONObject();
if (map != null && !map.isEmpty()) {
for (Map.Entry<String, String> entry : map.entrySet()) {
data.put(entry.getKey(), entry.getValue());
}
}
return data;
}
private static JSONArray fromCollection(Collection<String> collection) {
JSONArray data = new JSONArray();
if (collection != null && !collection.isEmpty()) {
for (String s : collection) {
data.put(s);
}
}
return data;
}
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
Log.v(TAG, ">>>>>> initialize cordova = "+cordova); handleNotificationMessage(NotificationManager.extractMessage(cordova.getActivity().getIntent()));
}
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleNotificationMessage(NotificationManager.extractMessage(intent));
}
private void handleNotificationMessage(@Nullable NotificationMessage message) {
if (message != null) {
// Open from push
PluginResult result;
try {
JSONObject eventArgs = new JSONObject();
eventArgs.put("timeStamp", System.currentTimeMillis());
JSONObject values = new JSONObject(message.payload());
if (message.url() != null) {
values.put("url", message.url());
}
switch (message.type()) {
case OTHER:
values.put("type", "other");
break;
case CLOUD_PAGE:
values.put("type", "cloudPage");
break;
case OPEN_DIRECT:
values.put("type", "openDirect");
break;
}
eventArgs.put("values", values);
eventArgs.put("type", "notificationOpened");
result = new PluginResult(PluginResult.Status.OK, eventArgs);
result.setKeepCallback(true);
if (eventsChannel != null && notificationOpenedSubscribed) {
eventsChannel.sendPluginResult(result);
} else {
cachedNotificationOpenedResult = result;
}
} catch (Exception e) {
// NO_OP
}
}
}
@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext)
throws JSONException {
Log.v(TAG, ">> Inside execute >> action = "+action);
if (handleStaticAction(action, args, callbackContext)) {
return true;
}
final ActionHandler handler = getActionHandler(action);
if (handler == null) {
Log.v(TAG, ">> Inside execute >> handler == null");
return false;
}
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
if (MarketingCloudSdk.isReady()) {
Log.v(TAG, ">> MarketingCloudSdk.isReady");
handler.execute(MarketingCloudSdk.getInstance(), args, callbackContext);
} else if (MarketingCloudSdk.isInitializing()) {
Log.v(TAG, ">> MarketingCloudSdk.isInitializing");
MarketingCloudSdk.requestSdk(new MarketingCloudSdk.WhenReadyListener() {
@Override
public void ready(@NonNull MarketingCloudSdk sdk) {
handler.execute(sdk, args, callbackContext);
}
});
} else {
Log.v(TAG, ">> MarketingCloudSdk#init has not been called");
callbackContext.error("MarketingCloudSdk#init has not been called");
}
}
});
return true;
}
private boolean handleStaticAction(String action, JSONArray args, CallbackContext callbackContext) {
Log.v(TAG, ">> handleStaticAction >> switch >> action = "+action);
switch (action) {
case "enableVerboseLogging":
Log.v(TAG, ">> enableVerboseLogging");
MarketingCloudSdk.setLogLevel(MCLogListener.VERBOSE);
MarketingCloudSdk.setLogListener(new MCLogListener.AndroidLogListener());
callbackContext.success();
return true;
case "disableVerboseLogging":
Log.v(TAG, ">> disableVerboseLogging");
MarketingCloudSdk.setLogListener(null);
callbackContext.success();
return true;
case "registerEventsChannel":
Log.v(TAG, ">> registerEventsChannel");
registerEventsChannel(callbackContext);
return true;
case "subscribe":
Log.v(TAG, ">> subscribe");
subscribe(args, callbackContext);
return true;
default:
return false;
}
}
private void registerEventsChannel(CallbackContext callbackContext) {
this.eventsChannel = callbackContext;
if (notificationOpenedSubscribed) {
Log.v(TAG, ">> inside registerEventsChannel");
sendCachedPushEvent(eventsChannel);
}
}
private void subscribe(JSONArray args, CallbackContext context) {
switch (args.optString(0, null)) {
case "notificationOpened":
notificationOpenedSubscribed = true;
if (eventsChannel != null) {
sendCachedPushEvent(eventsChannel);
}
break;
default:
// NO_OP
}
}
private void sendCachedPushEvent(CallbackContext callbackContext) {
if (cachedNotificationOpenedResult != null) {
Log.v(TAG, ">> inside sendCachedPushEvent");
callbackContext.sendPluginResult(cachedNotificationOpenedResult);
cachedNotificationOpenedResult = null;
}
}
private ActionHandler getActionHandler(String action) {
Log.v(TAG, ">> getActionHandler >> switch >> action = "+action);
switch (action) {
case "getSystemToken":
return getSystemToken();
case "isPushEnabled":
return isPushEnabled();
case "enablePush":
return enabledPush();
case "disablePush":
return disablePush();
case "getAttributes":
return getAttributes();
case "setAttribute":
return setAttribute();
case "clearAttribute":
return clearAttribute();
case "addTag":
return addTag();
case "removeTag":
return removeTag();
case "getTags":
return getTags();
case "getDeviceId":
return getDeviceId();
case "setContactKey":
return setContactKey();
case "getContactKey":
return getContactKey();
default:
return null;
}
}
private ActionHandler getContactKey() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) { callbackContext.success(sdk.getRegistrationManager().getContactKey());
}
};
}
private ActionHandler setContactKey() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
String contactKey = args.optString(0, null);
boolean success = sdk.getRegistrationManager().edit().setContactKey(contactKey).commit();
callbackContext.success(success ? 1 : 0);
}
};
}
private ActionHandler getTags() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
callbackContext.success(fromCollection(sdk.getRegistrationManager().getTags()));
}
};
}
private ActionHandler removeTag() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
String tag = args.optString(0, null);
boolean success = sdk.getRegistrationManager().edit().removeTag(tag).commit();
callbackContext.success(success ? 1 : 0);
}
};
}
private ActionHandler addTag() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
String tag = args.optString(0, null);
boolean success = sdk.getRegistrationManager().edit().addTag(tag).commit();
callbackContext.success(success ? 1 : 0);
}
};
}
private ActionHandler clearAttribute() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
String key = args.optString(0, null);
boolean success = sdk.getRegistrationManager().edit().clearAttribute(key).commit();
callbackContext.success(success ? 1 : 0);
}
};
}
private ActionHandler setAttribute() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
String key = args.optString(0, null);
String value = args.optString(1);
boolean success = sdk.getRegistrationManager().edit().setAttribute(key, value).commit();
callbackContext.success(success ? 1 : 0);
}
};
}
private ActionHandler getAttributes() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
try {
callbackContext.success(fromMap(sdk.getRegistrationManager().getAttributes()));
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
};
}
private ActionHandler disablePush() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
sdk.getPushMessageManager().disablePush();
callbackContext.success();
}
};
}
private ActionHandler enabledPush() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
sdk.getPushMessageManager().enablePush();
callbackContext.success();
}
};
}
private ActionHandler isPushEnabled() {
Log.v(TAG, ">> Inside isPushEnabled");
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
Log.v(TAG, ">> ActionHandler isPushEnabled() ="+sdk.getPushMessageManager().isPushEnabled());
callbackContext.success(sdk.getPushMessageManager().isPushEnabled() ? 1 : 0);
}
};
}
private ActionHandler getSystemToken() {
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
callbackContext.success(sdk.getPushMessageManager().getPushToken());
}
};
}
private ActionHandler getDeviceId() {
Log.v(TAG, ">> Inside getDeviceID");
return new ActionHandler() {
@Override
public void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext) {
Log.v(TAG, ">> ActionHandler getDeviceId = "+sdk.getRegistrationManager().getDeviceId());
callbackContext.success(sdk.getRegistrationManager().getDeviceId());
}
};
}
interface ActionHandler {
void execute(MarketingCloudSdk sdk, JSONArray args, CallbackContext callbackContext);
}
}
Please advise