NotificationUtils.java
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
}
}