private void generateMessagingStyleNotification()

in WearAccessibilityApp/Wearable/src/main/java/com/example/android/wearable/wear/wearaccessibilityapp/NotificationsActivity.java [147:313]


        private void generateMessagingStyleNotification(Context context) {
            Log.d(TAG, "generateMessagingStyleNotification()");

            // Main steps for building a MESSAGING_STYLE notification:
            //      0. Get your data
            //      1. Create/Retrieve Notification Channel for O and beyond devices (26+)
            //      2. Build the MESSAGING_STYLE
            //      3. Set up main Intent for notification
            //      4. Set up RemoteInput (users can input directly from notification)
            //      5. Build and issue the notification

            // 0. Get your data (everything unique per Notification).
            MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData =
                    MockDatabase.getMessagingStyleData(getContext());

            // 1. Create/Retrieve Notification Channel for O and beyond devices (26+).
            String notificationChannelId = NotificationUtil.createNotificationChannel(
                    getContext(),
                    messagingStyleCommsAppData);


            // 2. Build the Notification.Style (MESSAGING_STYLE).
            String contentTitle = messagingStyleCommsAppData.getContentTitle();

            MessagingStyle messagingStyle =
                    new NotificationCompat.MessagingStyle(messagingStyleCommsAppData.getMe())
                            /*
                             * <p>This API's behavior was changed in SDK version
                             * {@link Build.VERSION_CODES#P}. If your application's target version
                             * is less than {@link Build.VERSION_CODES#P}, setting a conversation
                             * title to a non-null value will make {@link #isGroupConversation()}
                             * return {@code true} and passing {@code null} will make it return
                             * {@code false}.
                             * This behavior can be overridden by calling
                             * {@link #setGroupConversation(boolean)} regardless of SDK version.
                             * In {@code P} and above, this method does not affect group
                             * conversation settings.
                             *
                             * In our case, we use the same title.
                             */
                            .setConversationTitle(contentTitle);

            // Adds all Messages.
            // Note: Messages include the text, timestamp, and sender.
            for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
                messagingStyle.addMessage(message);
            }

            messagingStyle.setGroupConversation(messagingStyleCommsAppData.isGroupConversation());

            // 3. Set up main Intent for notification.
            Intent notifyIntent = new Intent(getActivity(), MessagingMainActivity.class);

            PendingIntent mainPendingIntent =
                    PendingIntent.getActivity(
                            getActivity(),
                            0,
                            notifyIntent,
                            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.
            // Note, this is only used if the user has selected an action in UI.

            // Create the RemoteInput specifying this key.
            String replyLabel = getString(R.string.reply_label);
            RemoteInput remoteInput = new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY)
                    .setLabel(replyLabel)
                    // Use machine learning to create responses based on previous messages.
                    .setChoices(messagingStyleCommsAppData.getReplyChoicesBasedOnLastMessage())
                    .build();

            // Create PendingIntent for service that handles input.
            Intent replyIntent = new Intent(getActivity(), MessagingIntentService.class);
            replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
            PendingIntent replyActionPendingIntent =
                    PendingIntent.getService(getActivity(), 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.reply,
                            replyLabel,
                            replyActionPendingIntent)
                            .addRemoteInput(remoteInput)
                            // Informs system we aren't bringing up our own custom UI for a reply
                            // action.
                            .setShowsUserInterface(false)
                            // Allows system to generate replies by context of conversation.
                            .setAllowGeneratedReplies(true)
                            // Add WearableExtender to enable inline actions.
                            .setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
                            .extend(inlineActionForWear2)
                            .build();


            // 5. Build and issue the notification.

            // Because we want this to be a new notification (not updating current notification), we
            // create a new Builder. Later, we update this same notification, so we need to save
            // this Builder globally (as outlined earlier).

            // Notification Channel Id is ignored for Android pre O (26).
            NotificationCompat.Builder notificationCompatBuilder =
                    new NotificationCompat.Builder(
                            context, notificationChannelId);

            GlobalNotificationBuilder.setNotificationCompatBuilderInstance(
                    notificationCompatBuilder);

            int iconToUse = mAvatarOn ? R.drawable.avatar : R.drawable.watch;


            notificationCompatBuilder
                    // MESSAGING_STYLE sets title and content for Wear 1.+ and Wear 2.0 devices.
                    .setStyle(messagingStyle)
                    .setContentTitle(contentTitle)
                    .setContentText(messagingStyleCommsAppData.getContentText())
                    .setSmallIcon(iconToUse)
                    .setLargeIcon(BitmapFactory.decodeResource(
                            getResources(),
                            iconToUse))
                    .setContentIntent(mainPendingIntent)
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    // Set primary color (important for Wear 2.0 Notifications).
                    .setColor(ContextCompat.getColor(context, R.color.background))

                    // Number of new notifications for API <24 (Wear 1.+) devices.
                    .setSubText(
                            Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))

                    .setCategory(Notification.CATEGORY_MESSAGE)

                    // 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(messagingStyleCommsAppData.getPriority())

                    // Sets lock-screen visibility for 25 and below. For 26 and above, lock screen
                    // visibility is set in the NotificationChannel.
                    .setVisibility(messagingStyleCommsAppData.getChannelLockscreenVisibility());

            if (mActionOn) {
                notificationCompatBuilder.addAction(replyAction);
            }

            // If the phone is in "Do not disturb" mode, the user may still be notified if the
            // sender(s) are in a group allowed through "Do not disturb" by the user.
            for (Person person : messagingStyleCommsAppData.getParticipants()) {
                notificationCompatBuilder.addPerson(person.getUri());
            }

            Notification notification = notificationCompatBuilder.build();
            mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);

            // Close app to demonstrate notification in steam.
            getActivity().finish();
        }