关于Notification显示网络图片的Glide使用

public class NotificationUtils {
    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    private  Notification.Builder builder;
    private NotificationCompat.Builder builder1;

    //单例对象
    private static NotificationUtils instance;


    public NotificationUtils(){

    }

    public static NotificationUtils getInstance() {
        if (instance == null) {
            synchronized (NotificationUtils.class) {
                if (instance == null) {
                    instance = new NotificationUtils();
                }
            }
        }
        return instance;
    }


    @SuppressLint("NewApi")
    public void createNotificationChannel(Context context){
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager(context).createNotificationChannel(channel);
    }
    private NotificationManager getManager(Context context){
        if (manager == null){
            manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }
    @SuppressLint("NewApi")
    public Notification.Builder getChannelNotification(Context context,RemoteViews mRemoteViews){
        Notification.Builder builder=new Notification.Builder(context,id);
        Resources res = context.getResources();
        Bitmap bmp= BitmapFactory.decodeResource(res, R.drawable.ic_icon);
        builder.setLargeIcon(bmp);
        builder.setSmallIcon(R.drawable.ic_icon);
        builder.setCustomContentView(mRemoteViews);
        builder.setCustomBigContentView(mRemoteViews);
        builder.setAutoCancel(false);
        return builder;
    }
    public NotificationCompat.Builder getNotification_25(Context context,RemoteViews mRemoteViews){
        final NotificationCompat.Builder builder=new NotificationCompat.Builder(context);
        builder.setSmallIcon(R.drawable.ic_icon);
        Resources res = context.getResources();
        Bitmap bmp= BitmapFactory.decodeResource(res, R.drawable.ic_icon);
        builder.setLargeIcon(bmp);
        builder.setCustomContentView(mRemoteViews);
        builder.setCustomBigContentView(mRemoteViews);
        builder.setAutoCancel(false);
        return builder;
    }

    public void sendNotification(final PlayingMusicBeens playingMusicBeens, final int num, final Context context){
        Glide.with(context)
                .asBitmap()
                .load(playingMusicBeens.getPic())
                .into(new SimpleTarget() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) {

                        Intent intent = new Intent(context, MainAcvity.class);//将要跳转的界面
                        PendingIntent intentPend = PendingIntent.getActivity(context, num, intent, PendingIntent.FLAG_CANCEL_CURRENT);

                        final RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_play);
                        mRemoteViews.setTextViewText(R.id.tv_name, playingMusicBeens.getMusicname());
                        mRemoteViews.setTextViewText(R.id.tv_name, playingMusicBeens.getMusicname()+"-"+playingMusicBeens.getMusic_singer());
                        mRemoteViews.setImageViewResource(R.id.ib_last,R.drawable.ic_lrc_last);
                        if(PlayController.getInstance(context).get_state()==0){
                            mRemoteViews.setImageViewResource(R.id.ib_play,R.drawable.ic_lrc_stop);
                        }else {
                            mRemoteViews.setImageViewResource(R.id.ib_play,R.drawable.ic_lrc_play);
                        }

                        mRemoteViews.setImageViewResource(R.id.ib_next,R.drawable.ic_lrc_next);
                        mRemoteViews.setImageViewBitmap(R.id.iv_img,resource);

                        Intent intentLast = new Intent("last");
                        PendingIntent pIntentLast = PendingIntent.getBroadcast(context.getApplicationContext(), 0,
                                intentLast, 0);
                        mRemoteViews.setOnClickPendingIntent(R.id.ib_last,pIntentLast);

                        Intent intentPause = new Intent("play");
                        PendingIntent pIntentPause = PendingIntent.getBroadcast(context.getApplicationContext(), 0,
                                intentPause, 0);
                        mRemoteViews.setOnClickPendingIntent(R.id.ib_play,pIntentPause);

                        Intent intentNext = new Intent("next");
                        PendingIntent pIntentNext = PendingIntent.getBroadcast(context.getApplicationContext(), 0,
                                intentNext, 0);
                        mRemoteViews.setOnClickPendingIntent(R.id.ib_next,pIntentNext);


                        if (Build.VERSION.SDK_INT >= 26) {
                            if(builder!=null){
                                builder.setCustomContentView(mRemoteViews);
                            }else{
                                createNotificationChannel(context);
                                builder = getChannelNotification(context,mRemoteViews);
                                if(!isAppForeground(context)){
                                    builder.setContentIntent(intentPend);
                                }
                            }
                            getManager(context).notify("notification",111, builder.build());
                        } else {
                            if(builder1!=null){
                                builder1.setCustomContentView(mRemoteViews);
                            }else{
                                builder1 = getNotification_25(context,mRemoteViews);
                                if(!isAppForeground(context)){
                                    builder1.setContentIntent(intentPend);
                                }

                            }
                            getManager(context).notify("notification",111, builder1.build());
                        }
                    }
                });
    }



    //判断通知权限是否开启
    @SuppressLint("NewApi")
    public  boolean isNotificationEnabled(Context context) {
        AppOpsManager mAppOps =
                (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        Class appOpsClass = null;
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());

            Method checkOpNoThrowMethod =
                    appOpsClass.getMethod("checkOpNoThrow",
                            Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
            int value = (Integer) opPostNotificationValue.get(Integer.class);

            return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) ==
                    AppOpsManager.MODE_ALLOWED);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

  //判断当前app是否在前台
    private static boolean isAppForeground(Context context) {
        ActivityManager activityManager =
                (ActivityManager)context.getSystemService(Service.ACTIVITY_SERVICE);
        List runningAppProcessInfoList =
                activityManager.getRunningAppProcesses();
        if (runningAppProcessInfoList == null) {
            Log.d("isAppForeground","runningAppProcessInfoList is null!");
            return false;
        }

        for(ActivityManager.RunningAppProcessInfo processInfo : runningAppProcessInfoList) {
            if (processInfo.processName.equals(context.getPackageName())
                    &&(processInfo.importance ==
                    ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)) {
                return true;
            }
        }
        return false;
    }

}

使用

  NotificationUtils.getInstance().sendNotification(playingMusicBeens,1,context);

你可能感兴趣的:(关于Notification显示网络图片的Glide使用)