NotificationUtils.java 2.23 KB
package com.lotus.town.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.lotus.town.R;


public class NotificationUtils {
    private static String NOTIFICATION_TAG = "locker";
    private static int NOTIFICATION_ID = 111;

    public static void sendNotification(Context context,Class clz){
        if(Build.VERSION.SDK_INT <= 23){
            return;
        }
            Intent i = new Intent();
            i.setClass(context,clz);
            PendingIntent pIntent = PendingIntent.getActivity(context,1002,i,PendingIntent.FLAG_ONE_SHOT);
//        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        context.startActivity(i);
            NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            String channelId = ensureNotificationChannel(context,notificationManager);
            notificationManager.cancel(NOTIFICATION_TAG,NOTIFICATION_ID);
            notificationManager.notify(NOTIFICATION_TAG,NOTIFICATION_ID,new NotificationCompat.Builder(context,channelId).setSmallIcon(R.drawable.empty).setFullScreenIntent(pIntent,true).build());
    }

    private static String ensureNotificationChannel(Context context, NotificationManager notificationManager) {
        String notificationChannelId = "locker";
        if(Build.VERSION.SDK_INT >= 26  && notificationManager.getNotificationChannel(notificationChannelId) == null){
            NotificationChannel channel = new NotificationChannel(notificationChannelId,context.getString(R.string.app_name),NotificationManager.IMPORTANCE_HIGH);
            channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
            notificationManager.createNotificationChannel(channel);
        }
        return "locker";
    }

    public static void clearNotification(Context context){
        if(Build.VERSION.SDK_INT <= 23){
            return;
        }
        ((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(NOTIFICATION_TAG,NOTIFICATION_ID);
    }
}