|
17 | 17 | import androidx.core.content.ContextCompat; |
18 | 18 |
|
19 | 19 | public class AppNotificationManager { |
20 | | - private static final String CHANNEL_ID = "cht_task_notifications"; |
21 | | - private static final String CHANNEL_NAME = "CHT Task Notifications"; |
22 | | - private static final int REQUEST_NOTIFICATION_PERMISSION = 1001; |
| 20 | + private static final String CHANNEL_ID = "cht_task_notifications"; |
| 21 | + private static final String CHANNEL_NAME = "CHT Task Notifications"; |
| 22 | + private static final int REQUEST_NOTIFICATION_PERMISSION = 1001; |
23 | 23 |
|
24 | | - private final Context context; |
25 | | - NotificationManager manager; |
| 24 | + private final Context context; |
| 25 | + NotificationManager manager; |
26 | 26 |
|
27 | | - public AppNotificationManager(Context context) { |
28 | | - this.context = context; |
29 | | - manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
30 | | - createNotificationChannel(); |
31 | | - } |
| 27 | + public AppNotificationManager(Context context) { |
| 28 | + this.context = context; |
| 29 | + manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 30 | + createNotificationChannel(); |
| 31 | + } |
32 | 32 |
|
33 | | - public boolean hasNotificationPermission() { |
34 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
35 | | - return ContextCompat.checkSelfPermission(context, |
36 | | - Manifest.permission.POST_NOTIFICATIONS) |
37 | | - == PackageManager.PERMISSION_GRANTED; |
| 33 | + public boolean hasNotificationPermission() { |
| 34 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 35 | + return ContextCompat.checkSelfPermission(context, |
| 36 | + Manifest.permission.POST_NOTIFICATIONS) |
| 37 | + == PackageManager.PERMISSION_GRANTED; |
| 38 | + } |
| 39 | + //versions below 13 |
| 40 | + return true; |
38 | 41 | } |
39 | | - //versions below 13 |
40 | | - return true; |
41 | | - } |
42 | 42 |
|
43 | | - public void requestNotificationPermission() { |
44 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && |
45 | | - context instanceof Activity activity && !hasNotificationPermission()) { |
46 | | - ActivityCompat.requestPermissions(activity, |
47 | | - new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATION_PERMISSION); |
| 43 | + public void requestNotificationPermission() { |
| 44 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && |
| 45 | + context instanceof Activity activity && !hasNotificationPermission()) { |
| 46 | + ActivityCompat.requestPermissions(activity, |
| 47 | + new String[]{Manifest.permission.POST_NOTIFICATIONS}, REQUEST_NOTIFICATION_PERMISSION); |
| 48 | + } |
48 | 49 | } |
49 | | - } |
50 | 50 |
|
51 | | - void showNotification(int id, String title, String contentText) { |
52 | | - Intent intent = new Intent(context, EmbeddedBrowserActivity.class); |
53 | | - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
54 | | - PendingIntent pendingIntent = PendingIntent.getActivity( |
55 | | - context, |
56 | | - 0, |
57 | | - intent, |
58 | | - PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE |
59 | | - ); |
60 | | - NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) |
61 | | - .setSmallIcon(R.drawable.ic_notification) |
62 | | - .setContentTitle(title) |
63 | | - .setContentText(contentText) |
64 | | - .setAutoCancel(true) |
65 | | - .setContentIntent(pendingIntent) |
66 | | - .setPriority(NotificationCompat.PRIORITY_DEFAULT); |
| 51 | + void showNotification(int id, String title, String contentText) { |
| 52 | + Intent intent = new Intent(context, EmbeddedBrowserActivity.class); |
| 53 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 54 | + PendingIntent pendingIntent = PendingIntent.getActivity( |
| 55 | + context, |
| 56 | + 0, |
| 57 | + intent, |
| 58 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE |
| 59 | + ); |
| 60 | + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID) |
| 61 | + .setSmallIcon(R.drawable.ic_notification) |
| 62 | + .setContentTitle(title) |
| 63 | + .setContentText(contentText) |
| 64 | + .setAutoCancel(true) |
| 65 | + .setContentIntent(pendingIntent) |
| 66 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT); |
67 | 67 |
|
68 | 68 |
|
69 | | - manager.notify(id, builder.build()); |
70 | | - } |
| 69 | + manager.notify(id, builder.build()); |
| 70 | + } |
71 | 71 |
|
72 | | - private void createNotificationChannel() { |
73 | | - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
74 | | - NotificationChannel notificationChannel = new NotificationChannel( |
75 | | - CHANNEL_ID, |
76 | | - CHANNEL_NAME, |
77 | | - NotificationManager.IMPORTANCE_DEFAULT |
78 | | - ); |
79 | | - manager.createNotificationChannel(notificationChannel); |
| 72 | + private void createNotificationChannel() { |
| 73 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 74 | + NotificationChannel notificationChannel = new NotificationChannel( |
| 75 | + CHANNEL_ID, |
| 76 | + CHANNEL_NAME, |
| 77 | + NotificationManager.IMPORTANCE_DEFAULT |
| 78 | + ); |
| 79 | + manager.createNotificationChannel(notificationChannel); |
| 80 | + } |
80 | 81 | } |
81 | | - } |
82 | 82 |
|
83 | 83 | } |
0 commit comments