Android 安装推送插件时,我的自定义Cordova插件终止的粘性通知

Android 安装推送插件时,我的自定义Cordova插件终止的粘性通知,android,cordova,phonegap-plugins,android-notifications,Android,Cordova,Phonegap Plugins,Android Notifications,我写的自定义插件,它只适用于处理粘性通知插件事件 我得到的工作场景和问题:- 当安装在demo corodva应用程序中时,它可以正常工作,功能也可以正常工作,就像关闭应用程序后,由自定义插件创建的粘性通知出现在locak屏幕和通知栏上 但当尝试在已安装自定义插件的演示应用程序中安装phonegap push插件时。当用户关闭应用程序时,粘性通知从通知栏中清除/删除/消失 当我看到android的Phonegap推送插件源代码时,我已经在插件中编写了NotifcationManger.cance

我写的自定义插件,它只适用于处理粘性通知插件事件

我得到的工作场景和问题:-

  • 当安装在demo corodva应用程序中时,它可以正常工作,功能也可以正常工作,就像关闭应用程序后,由自定义插件创建的粘性通知出现在locak屏幕和通知栏上
  • 但当尝试在已安装自定义插件的演示应用程序中安装phonegap push插件时。当用户关闭应用程序时,粘性通知从通知栏中清除/删除/消失
  • 当我看到android的Phonegap推送插件源代码时,我已经在插件中编写了NotifcationManger.cancelAll()

    不明白为什么会这样

    在下面添加我的粘性通知插件代码:-

    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);
    }}
    

    按下home(主页)按钮后,删除取消通知的两行即可解决您的问题-

    这两行可以在PushPlugin.java文件中的onPause()方法中找到

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

    什么是“清除”?粘性通知清除/取消/从通知栏中消失可能需要显示一些代码。我添加了我的Cordova插件类