in Notifications/Wearable/src/main/java/com/example/android/wearable/wear/wearnotifications/StandaloneMainActivity.java [370:501]
private void generateBigPictureStyleNotification() {
Log.d(TAG, "generateBigPictureStyleNotification()");
// Main steps for building a BIG_PICTURE_STYLE notification:
// 0. Get your data
// 1. Create/Retrieve Notification Channel for O and beyond devices (26+)
// 2. Build the BIG_PICTURE_STYLE
// 3. Set up main Intent for notification
// 4. Set up RemoteInput, so users can input (keyboard and voice) from notification
// 5. Build and issue the notification
// 0. Get your data (everything unique per Notification).
MockDatabase.BigPictureStyleSocialAppData bigPictureStyleSocialAppData =
MockDatabase.getBigPictureStyleData();
// 1. Build the BIG_PICTURE_STYLE.
BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle()
// Provides the bitmap for the BigPicture notification.
.bigPicture(
BitmapFactory.decodeResource(
getResources(),
bigPictureStyleSocialAppData.getBigImage()))
// Overrides ContentTitle in the big form of the template.
.setBigContentTitle(bigPictureStyleSocialAppData.getBigContentTitle())
// Summary line after the detail section in the big form of the template.
.setSummaryText(bigPictureStyleSocialAppData.getSummaryText());
// 2. Create/Retrieve Notification Channel for O and beyond devices (26+).
String notificationChannelId =
NotificationUtil.createNotificationChannel(this, bigPictureStyleSocialAppData);
// 3. Set up main Intent for notification.
Intent mainIntent = new Intent(this, BigPictureSocialMainActivity.class);
PendingIntent mainPendingIntent =
PendingIntent.getActivity(
this,
0,
mainIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// 4. Set up a RemoteInput Action, so users can input (keyboard, drawing, voice) directly
// from the notification without entering the app.
// Create the RemoteInput.
String replyLabel = getString(R.string.reply_label);
RemoteInput remoteInput =
new RemoteInput.Builder(BigPictureSocialIntentService.EXTRA_COMMENT)
.setLabel(replyLabel)
// List of quick response choices for any wearables paired with the phone.
.setChoices(bigPictureStyleSocialAppData.getPossiblePostResponses())
.build();
// Create PendingIntent for service that handles input.
Intent replyIntent = new Intent(this, BigPictureSocialIntentService.class);
replyIntent.setAction(BigPictureSocialIntentService.ACTION_COMMENT);
PendingIntent replyActionPendingIntent = PendingIntent.getService(this, 0, replyIntent, 0);
// Enable action to appear inline on Wear 2.0 (24+). This means it will appear over the
// lower portion of the Notification for easy action (only possible for one action).
final NotificationCompat.Action.WearableExtender inlineActionForWear2 =
new NotificationCompat.Action.WearableExtender()
.setHintDisplayActionInline(true)
.setHintLaunchesActivity(false);
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(
R.drawable.ic_reply_white_18dp,
replyLabel,
replyActionPendingIntent)
.addRemoteInput(remoteInput)
// Add WearableExtender to enable inline actions.
.extend(inlineActionForWear2)
.build();
// 5. Build and issue the notification
// Because we want this to be a new notification (not updating a previous notification), we
// create a new Builder. Later, we use the same global builder to get back the notification
// we built here for a comment on the post.
// Notification Channel Id is ignored for Android pre O (26).
NotificationCompat.Builder notificationCompatBuilder =
new NotificationCompat.Builder(
getApplicationContext(), notificationChannelId);
GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
notificationCompatBuilder
// BIG_PICTURE_STYLE sets title and content.
.setStyle(bigPictureStyle)
.setContentTitle(bigPictureStyleSocialAppData.getContentTitle())
.setContentText(bigPictureStyleSocialAppData.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))
.setSubText(Integer.toString(1))
.addAction(replyAction)
.setCategory(Notification.CATEGORY_SOCIAL)
// 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(bigPictureStyleSocialAppData.getPriority())
// Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
// visibility is set in the NotificationChannel.
.setVisibility(bigPictureStyleSocialAppData.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 : bigPictureStyleSocialAppData.getParticipants()) {
notificationCompatBuilder.addPerson(name);
}
Notification notification = notificationCompatBuilder.build();
mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);
// Close app to demonstrate notification in steam.
finish();
}