in Notifications/Wearable/src/main/java/com/example/android/wearable/wear/wearnotifications/StandaloneMainActivity.java [506:604]
private void generateInboxStyleNotification() {
Log.d(TAG, "generateInboxStyleNotification()");
// Main steps for building a INBOX_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the INBOX_STYLE
// 3. Set up main Intent for notification
// 4. Build and issue the notification
// 0. Get your data (everything unique per Notification).
MockDatabase.InboxStyleEmailAppData inboxStyleEmailAppData =
MockDatabase.getInboxStyleData();
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, inboxStyleEmailAppData);
// 2. Build the INBOX_STYLE
InboxStyle inboxStyle = new NotificationCompat.InboxStyle()
// This title is slightly different than regular title, since I know INBOX_STYLE is
// available.
.setBigContentTitle(inboxStyleEmailAppData.getBigContentTitle())
.setSummaryText(inboxStyleEmailAppData.getSummaryText());
// Add each summary line of the new emails, you can add up to 5.
for (String summary : inboxStyleEmailAppData.getIndividualEmailSummary()) {
inboxStyle.addLine(summary);
}
// 3. Set up main Intent for notification.
Intent mainIntent = new Intent(this, InboxMainActivity.class);
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Build and issue the notification.
// Because we want this to be a new notification (not updating a previous notification), we
// create a new Builder. However, we don't need to update this notification later, so we
// will not need to set a global builder for access to the notification later.
// Notification Channel Id is ignored for Android pre O (26).
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(
getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
// INBOX_STYLE sets title and content.
.setStyle(inboxStyle)
.setContentTitle(inboxStyleEmailAppData.getContentTitle())
.setContentText(inboxStyleEmailAppData.getContentText())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(
getResources(),
R.drawable.ic_person_black_48dp))
.setContentIntent(mainPendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
// Set primary color (important for Wear 2.0 Notifications).
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
// Sets large number at the right-hand side of the notification for Wear 1.+.
.setSubText(Integer.toString(inboxStyleEmailAppData.getNumberOfNewEmails()))
.setCategory(Notification.CATEGORY_EMAIL)
// Sets priority for 25 and below. For 26 and above, 'priority' is deprecated for
// 'importance' which is set in the NotificationChannel. The integers representing
// 'priority' are different from 'importance', so make sure you don't mix them.
.setPriority(inboxStyleEmailAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(inboxStyleEmailAppData.getChannelLockscreenVisibility())
// Notifies system that the main launch intent is an Activity.
.extend(new NotificationCompat.WearableExtender()
.setHintContentIntentLaunchesActivity(true));
// If the phone is in "Do not disturb mode, the user will still be notified if
// the sender(s) is starred as a favorite.
for (String name : inboxStyleEmailAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
// Close app to demonstrate notification in steam.
finish();
}