Search code examples
androidcordovaphonegap-pluginsandroid-notifications

Sticky Notification of my Custom Cordova Plugin kill when installing push plugin


I am written on Custom plugin,Which works only for handling the sticky notification plugin events.

Working Scenario and Issues I am getting:-

  1. It works fine when install in the demo corodva application and functionality also works fine like after closing app sticky notification created by the custom plugin appear on locak screen and notification bar.
  2. But when trying to install phonegap push plugin in the demo application in which custom plugin already install. After that sticky notification gets clear/remove/disappear from notification bar.When the app closed by the user.

When I see the Phonegap Push Plugin source code for android the have written NotifcationManger.cancelAll() in the plugin.

Not getting why this is happen?

Adding my sticky notification plugin code below:-

public class StickyNotificationPlugin extends CordovaPlugin {
private static final String START_NOTIFICATION = "START";
private static final String STOP_NOTIFICATION = "STOP";
private Notification myNotication;
int intNotificationId = 11111;
private NotificationManager manager;
private SharedPreferences objSharedPreferences;

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    objSharedPreferences = this.cordova.getActivity().getSharedPreferences("My_Plugin_Prefs", Context.MODE_PRIVATE);

    System.out.println("Final in Plugin Action==>" + action);
    manager = (NotificationManager) this.cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);

    if (START_NOTIFICATION.equals(action)) {
        SharedPreferences.Editor objEditor = objSharedPreferences.edit();
        objEditor.putString("plugin_url", args.getString(0));
        objEditor.putString("plugin_token", args.getString(1));
        objEditor.putString("plugin_user_id", args.getString(2));
        objEditor.putBoolean("plugin_status", true);
        objEditor.commit();
        Notify();
    } else if (STOP_NOTIFICATION.equals(action)) {
        SharedPreferences.Editor objEditor = objSharedPreferences.edit();
        objEditor.putString("plugin_url", "");
        objEditor.putString("plugin_token", "");
        objEditor.putString("plugin_user_id", "");
        objEditor.putBoolean("plugin_status", false);
        objEditor.putLong("time", 0);
        objEditor.commit();
        manager.cancel(intNotificationId);
    }
    return true;
}

private void Notify() {
    Context objContext = this.cordova.getActivity();
    Intent objIntent = new Intent(objContext, ApiCallServeice.class);
    PendingIntent pi = PendingIntent.getService(objContext, intNotificationId, objIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    RemoteViews objRemoteViews = new RemoteViews(objContext.getApplicationContext().getPackageName(), R.layout.sticky_notification);
    objRemoteViews.setOnClickPendingIntent(R.id.btn_notification, pi);

    Notification.Builder builder = new Notification.Builder(objContext);
    builder.setAutoCancel(false);
    builder.setSmallIcon(objContext.getApplicationInfo().icon);
    objRemoteViews.setImageViewResource(R.id.img_icon, objContext.getApplicationInfo().icon);
    builder.setOngoing(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }
    builder.setContent(objRemoteViews);
    builder.build();

    myNotication = builder.getNotification();
    manager.notify(intNotificationId, myNotication);
}}

Solution

  • Your problem will be solved by removing the two lines that cancels the notifications once the home button is pressed -

    These two lines are found in the onPause() method in PushPlugin.java file.

    final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancelAll();