- depend on: implementation "com.android.support:support-compat:27.1.1"
Last active
September 14, 2018 19:03
-
-
Save OrenBochman/c8bceb1df69ece42ad5688642428224f to your computer and use it in GitHub Desktop.
All about Android Notifications
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
//CHANNEL_ID required bu android 8+ | |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.notification_icon) | |
.setContentTitle(textTitle) | |
.setContentText(textContent) | |
// bigger text are | |
.setStyle(new NotificationCompat.BigTextStyle().bigText("Much longer text that cannot fit one line...")) | |
// priority required by 7.1 or lower | |
.setPriority(NotificationCompat.PRIORITY_DEFAULT); |
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
private void createNotificationChannel() { | |
// Create the NotificationChannel, but only on API 26+ because | |
// the NotificationChannel class is new and not in the support library | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
CharSequence name = getString(R.string.channel_name); | |
String description = getString(R.string.channel_description); | |
int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); | |
channel.setDescription(description); | |
// Register the channel with the system; you can't change the importance | |
// or other notification behaviors after this | |
NotificationManager notificationManager = getSystemService(NotificationManager.class); | |
notificationManager.createNotificationChannel(channel); | |
} | |
} |
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
// Create an explicit intent for an Activity in your app | |
Intent intent = new Intent(this, AlertDetails.class); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); | |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.notification_icon) | |
.setContentTitle("My notification") | |
.setContentText("Hello World!") | |
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | |
// Set the intent that will fire when the user taps the notification | |
.setContentIntent(pendingIntent) | |
.setAutoCancel(true); |
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
... | |
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); | |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
mBuilder.setContentTitle("Picture Download") | |
.setContentText("Download in progress") | |
.setSmallIcon(R.drawable.ic_notification) | |
.setPriority(NotificationCompat.PRIORITY_LOW); | |
// Issue the initial notification with zero progress | |
int PROGRESS_MAX = 100; | |
int PROGRESS_CURRENT = 0; | |
mBuilder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false); | |
notificationManager.notify(notificationId, mBuilder.build()); | |
// Do the job here that tracks the progress. | |
// Usually, this should be in a worker thread | |
// To show progress, update PROGRESS_CURRENT and update the notification with: | |
// mBuilder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false); | |
// notificationManager.notify(notificationId, mBuilder.build()); | |
// When done, update the notification one more time to remove the progress bar | |
mBuilder.setContentText("Download complete") | |
.setProgress(0,0,false); | |
notificationManager.notify(notificationId, mBuilder.build()); |
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
// Create an Intent for the activity you want to start | |
Intent resultIntent = new Intent(this, ResultActivity.class); | |
// Create the TaskStackBuilder and add the intent, which inflates the back stack | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); | |
stackBuilder.addNextIntentWithParentStack(resultIntent); | |
// Get the PendingIntent containing the entire back stack | |
PendingIntent resultPendingIntent = | |
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
builder.setContentIntent(resultPendingIntent); | |
... | |
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); | |
notificationManager.notify(NOTIFICATION_ID, builder.build()); |
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
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"; | |
Notification newMessageNotification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.new_mail) | |
.setContentTitle(emailObject.getSenderName()) | |
.setContentText(emailObject.getSubject()) | |
.setLargeIcon(emailObject.getSenderAvatar()) | |
.setGroup(GROUP_KEY_WORK_EMAIL) | |
.build(); | |
//use constant ID for notification used as group summary | |
int SUMMARY_ID = 0; | |
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"; | |
Notification newMessageNotification1 = | |
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.ic_notify_email_status) | |
.setContentTitle(emailObject1.getSummary()) | |
.setContentText("You will not believe...") | |
.setGroup(GROUP_KEY_WORK_EMAIL) | |
.build(); | |
Notification newMessageNotification2 = | |
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) | |
.setSmallIcon(R.drawable.ic_notify_email_status) | |
.setContentTitle(emailObject2.getSummary()) | |
.setContentText("Please join us to celebrate the...") | |
.setGroup(GROUP_KEY_WORK_EMAIL) | |
.build(); | |
Notification summaryNotification = | |
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID) | |
.setContentTitle(emailObject.getSummary()) | |
//set content text to support devices running API level < 24 | |
.setContentText("Two new messages") | |
.setSmallIcon(R.drawable.ic_notify_summary_status) | |
//build summary info into InboxStyle template | |
.setStyle(new NotificationCompat.InboxStyle() | |
.addLine("Alex Faarborg Check this out") | |
.addLine("Jeff Chang Launch Party") | |
.setBigContentTitle("2 new messages") | |
.setSummaryText("[email protected]")) | |
//specify which group this notification belongs to | |
.setGroup(GROUP_KEY_WORK_EMAIL) | |
//set this notification as the summary for the group | |
.setGroupSummary(true) | |
.build(); | |
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); | |
notificationManager.notify(emailNotificationId1, newMessageNotification1); | |
notificationManager.notify(emailNotificationId2, newMessageNotification2); | |
notificationManager.notify(SUMMARY_ID, summaryNotification); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment