pop up wiindow with notification even apllication is not running

前端 未结 1 1751
遥遥无期
遥遥无期 2021-01-27 09:38

here is my code and I am getting my notification on time but the application is being ctrash during pop up

 private void showCustomPopupMenu()
{
    WindowManag         


        
相关标签:
1条回答
  • 2021-01-27 09:48

    You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert

      <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
      <uses-permission
            android:name="android.permission.INTERNAL_SYSTEM_WINDOW"
            tools:ignore="ProtectedPermissions" />
    

    set alert type of "TYPE_APPLICATION_OVERLAY".

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                }else{
                    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                }
    

    change your TYPE_PHONE with TYPE_SYSTEM_ALERT

    You should also refer this answer. If still you have doubt let me know.

    Now when notification arrived, check this:

      public void notificationArrived(String myMsg){
    
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    final boolean overlayEnabled = Settings.canDrawOverlays(MyFirebaseMessagingService.this);
                    Global.printLog("showTaskDetailPopup==", "overlayEnabled" + overlayEnabled);
    
                    if (!overlayEnabled) return;
                }
    
      new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
    
                    final Dialog dialog = new Dialog(MyFirebaseMessagingService.this);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.window_popup_medicine);
                    dialog.setCancelable(true);
    
                    TextView tv_msg = dialog.findViewById(R.id.tv_msg);
    
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
                        } else {
                                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
                        }
                        dialog.show();
                    }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
         }
    
    0 讨论(0)
提交回复
热议问题