Last active
October 4, 2023 13:19
-
-
Save igor-red/44978c596bd6bd322e78a3a99d5e07b8 to your computer and use it in GitHub Desktop.
Displaying local notification on all current Android versions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void createNotification(String title, String body) { | |
| final int NOTIFY_ID = 1002; | |
| // There are hardcoding only for show it's just strings | |
| String name = "my_package_channel"; | |
| String id = "my_package_channel_1"; // The user-visible name of the channel. | |
| String description = "my_package_first_channel"; // The user-visible description of the channel. | |
| Intent intent; | |
| PendingIntent pendingIntent; | |
| NotificationCompat.Builder builder; | |
| if (notifManager == null) { | |
| notifManager = | |
| (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); | |
| } | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| int importance = NotificationManager.IMPORTANCE_HIGH; | |
| NotificationChannel mChannel = notifManager.getNotificationChannel(id); | |
| if (mChannel == null) { | |
| mChannel = new NotificationChannel(id, name, importance); | |
| mChannel.setDescription(description); | |
| mChannel.enableVibration(true); | |
| mChannel.setLightColor(Color.GREEN); | |
| mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); | |
| notifManager.createNotificationChannel(mChannel); | |
| } | |
| builder = new NotificationCompat.Builder(this, id); | |
| intent = new Intent(this, MainActivity.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
| pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); | |
| builder.setContentTitle(title) // required | |
| .setSmallIcon(android.R.drawable.ic_popup_reminder) // required | |
| .setContentText(body) // required | |
| .setDefaults(Notification.DEFAULT_ALL) | |
| .setAutoCancel(true) | |
| .setContentIntent(pendingIntent) | |
| .setTicker(title) | |
| .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); | |
| } else { | |
| builder = new NotificationCompat.Builder(this); | |
| intent = new Intent(this, MainActivity.class); | |
| intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
| pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); | |
| builder.setContentTitle(title) // required | |
| .setSmallIcon(android.R.drawable.ic_popup_reminder) // required | |
| .setContentText(body) // required | |
| .setDefaults(Notification.DEFAULT_ALL) | |
| .setAutoCancel(true) | |
| .setContentIntent(pendingIntent) | |
| .setTicker(title) | |
| .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) | |
| .setPriority(Notification.PRIORITY_HIGH); | |
| } | |
| Notification notification = builder.build(); | |
| notifManager.notify(NOTIFY_ID, notification); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment