Java Code Examples for androidx.core.app.NotificationCompat.MessagingStyle#addMessage()

The following examples show how to use androidx.core.app.NotificationCompat.MessagingStyle#addMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessagingIntentService.java    From wear-os-samples with Apache License 2.0 4 votes vote down vote up
/** Handles action for replying to messages from the notification. */
private void handleActionReply(CharSequence replyCharSequence) {
    Log.d(TAG, "handleActionReply(): " + replyCharSequence);

    if (replyCharSequence != null) {

        // TODO: Asynchronously save your message to Database and servers.

        /*
         * You have two options for updating your notification (this class uses approach #2):
         *
         *  1. Use a new NotificationCompatBuilder to create the Notification. This approach
         *  requires you to get *ALL* the information that existed in the previous
         *  Notification (and updates) and pass it to the builder. This is the approach used in
         *  the MainActivity.
         *
         *  2. Use the original NotificationCompatBuilder to create the Notification. This
         *  approach requires you to store a reference to the original builder. The benefit is
         *  you only need the new/updated information. In our case, the reply from the user
         *  which we already have here.
         *
         *  IMPORTANT NOTE: You shouldn't save/modify the resulting Notification object using
         *  its member variables and/or legacy APIs. If you want to retain anything from update
         *  to update, retain the Builder as option 2 outlines.
         */

        // Retrieves NotificationCompat.Builder used to create initial Notification
        NotificationCompat.Builder notificationCompatBuilder =
                GlobalNotificationBuilder.getNotificationCompatBuilderInstance();

        // Recreate builder from persistent state if app process is killed
        if (notificationCompatBuilder == null) {
            // Note: New builder set globally in the method
            notificationCompatBuilder = recreateBuilderWithMessagingStyle();
        }

        // Since we are adding to the MessagingStyle, we need to first retrieve the
        // current MessagingStyle from the Notification itself.
        Notification notification = notificationCompatBuilder.build();
        MessagingStyle messagingStyle =
                NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
                        notification);

        // Add new message to the MessagingStyle. Set last parameter to null for responses
        // from user.
        messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);

        // Updates the Notification
        notification = notificationCompatBuilder.setStyle(messagingStyle).build();

        // Pushes out the updated Notification
        NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(NotificationsActivity.NOTIFICATION_ID, notification);
    }
}
 
Example 2
Source File: MessagingIntentService.java    From user-interface-samples with Apache License 2.0 4 votes vote down vote up
/** Handles action for replying to messages from the notification. */
private void handleActionReply(CharSequence replyCharSequence) {
    Log.d(TAG, "handleActionReply(): " + replyCharSequence);

    if (replyCharSequence != null) {

        // TODO: Asynchronously save your message to Database and servers.

        /*
         * You have two options for updating your notification (this class uses approach #2):
         *
         *  1. Use a new NotificationCompatBuilder to create the Notification. This approach
         *  requires you to get *ALL* the information that existed in the previous
         *  Notification (and updates) and pass it to the builder. This is the approach used in
         *  the MainActivity.
         *
         *  2. Use the original NotificationCompatBuilder to create the Notification. This
         *  approach requires you to store a reference to the original builder. The benefit is
         *  you only need the new/updated information. In our case, the reply from the user
         *  which we already have here.
         *
         *  IMPORTANT NOTE: You shouldn't save/modify the resulting Notification object using
         *  its member variables and/or legacy APIs. If you want to retain anything from update
         *  to update, retain the Builder as option 2 outlines.
         */

        // Retrieves NotificationCompat.Builder used to create initial Notification
        NotificationCompat.Builder notificationCompatBuilder =
                GlobalNotificationBuilder.getNotificationCompatBuilderInstance();

        // Recreate builder from persistent state if app process is killed
        if (notificationCompatBuilder == null) {
            // Note: New builder set globally in the method
            notificationCompatBuilder = recreateBuilderWithMessagingStyle();
        }

        // Since we are adding to the MessagingStyle, we need to first retrieve the
        // current MessagingStyle from the Notification itself.
        Notification notification = notificationCompatBuilder.build();
        MessagingStyle messagingStyle =
                NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
                        notification);

        // Add new message to the MessagingStyle. Set last parameter to null for responses
        // from user.
        messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);

        // Updates the Notification
        notification = notificationCompatBuilder.setStyle(messagingStyle).build();

        // Pushes out the updated Notification
        NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(StandaloneMainActivity.NOTIFICATION_ID, notification);
    }
}
 
Example 3
Source File: MessagingIntentService.java    From user-interface-samples with Apache License 2.0 4 votes vote down vote up
/** Handles action for replying to messages from the notification. */
private void handleActionReply(CharSequence replyCharSequence) {
    Log.d(TAG, "handleActionReply(): " + replyCharSequence);

    if (replyCharSequence != null) {

        // TODO: Asynchronously save your message to Database and servers.

        /*
         * You have two options for updating your notification (this class uses approach #2):
         *
         *  1. Use a new NotificationCompatBuilder to create the Notification. This approach
         *  requires you to get *ALL* the information that existed in the previous
         *  Notification (and updates) and pass it to the builder. This is the approach used in
         *  the MainActivity.
         *
         *  2. Use the original NotificationCompatBuilder to create the Notification. This
         *  approach requires you to store a reference to the original builder. The benefit is
         *  you only need the new/updated information. In our case, the reply from the user
         *  which we already have here.
         *
         *  IMPORTANT NOTE: You shouldn't save/modify the resulting Notification object using
         *  its member variables and/or legacy APIs. If you want to retain anything from update
         *  to update, retain the Builder as option 2 outlines.
         */

        // Retrieves NotificationCompat.Builder used to create initial Notification
        NotificationCompat.Builder notificationCompatBuilder =
                GlobalNotificationBuilder.getNotificationCompatBuilderInstance();

        // Recreate builder from persistent state if app process is killed
        if (notificationCompatBuilder == null) {
            // Note: New builder set globally in the method
            notificationCompatBuilder = recreateBuilderWithMessagingStyle();
        }

        // Since we are adding to the MessagingStyle, we need to first retrieve the
        // current MessagingStyle from the Notification itself.
        Notification notification = notificationCompatBuilder.build();
        MessagingStyle messagingStyle =
                NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
                        notification);

        // Add new message to the MessagingStyle. Set last parameter to null for responses
        // from user.
        messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);

        // Updates the Notification
        notification = notificationCompatBuilder.setStyle(messagingStyle).build();

        // Pushes out the updated Notification
        NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(MainActivity.NOTIFICATION_ID, notification);
    }
}
 
Example 4
Source File: MessagingIntentService.java    From android-Notifications with Apache License 2.0 4 votes vote down vote up
/** Handles action for replying to messages from the notification. */
private void handleActionReply(CharSequence replyCharSequence) {
    Log.d(TAG, "handleActionReply(): " + replyCharSequence);

    if (replyCharSequence != null) {

        // TODO: Asynchronously save your message to Database and servers.

        /*
         * You have two options for updating your notification (this class uses approach #2):
         *
         *  1. Use a new NotificationCompatBuilder to create the Notification. This approach
         *  requires you to get *ALL* the information that existed in the previous
         *  Notification (and updates) and pass it to the builder. This is the approach used in
         *  the MainActivity.
         *
         *  2. Use the original NotificationCompatBuilder to create the Notification. This
         *  approach requires you to store a reference to the original builder. The benefit is
         *  you only need the new/updated information. In our case, the reply from the user
         *  which we already have here.
         *
         *  IMPORTANT NOTE: You shouldn't save/modify the resulting Notification object using
         *  its member variables and/or legacy APIs. If you want to retain anything from update
         *  to update, retain the Builder as option 2 outlines.
         */

        // Retrieves NotificationCompat.Builder used to create initial Notification
        NotificationCompat.Builder notificationCompatBuilder =
                GlobalNotificationBuilder.getNotificationCompatBuilderInstance();

        // Recreate builder from persistent state if app process is killed
        if (notificationCompatBuilder == null) {
            // Note: New builder set globally in the method
            notificationCompatBuilder = recreateBuilderWithMessagingStyle();
        }

        // Since we are adding to the MessagingStyle, we need to first retrieve the
        // current MessagingStyle from the Notification itself.
        Notification notification = notificationCompatBuilder.build();
        MessagingStyle messagingStyle =
                NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
                        notification);

        // Add new message to the MessagingStyle. Set last parameter to null for responses
        // from user.
        messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);

        // Updates the Notification
        notification = notificationCompatBuilder.setStyle(messagingStyle).build();

        // Pushes out the updated Notification
        NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(StandaloneMainActivity.NOTIFICATION_ID, notification);
    }
}
 
Example 5
Source File: MessagingIntentService.java    From android-Notifications with Apache License 2.0 4 votes vote down vote up
/** Handles action for replying to messages from the notification. */
private void handleActionReply(CharSequence replyCharSequence) {
    Log.d(TAG, "handleActionReply(): " + replyCharSequence);

    if (replyCharSequence != null) {

        // TODO: Asynchronously save your message to Database and servers.

        /*
         * You have two options for updating your notification (this class uses approach #2):
         *
         *  1. Use a new NotificationCompatBuilder to create the Notification. This approach
         *  requires you to get *ALL* the information that existed in the previous
         *  Notification (and updates) and pass it to the builder. This is the approach used in
         *  the MainActivity.
         *
         *  2. Use the original NotificationCompatBuilder to create the Notification. This
         *  approach requires you to store a reference to the original builder. The benefit is
         *  you only need the new/updated information. In our case, the reply from the user
         *  which we already have here.
         *
         *  IMPORTANT NOTE: You shouldn't save/modify the resulting Notification object using
         *  its member variables and/or legacy APIs. If you want to retain anything from update
         *  to update, retain the Builder as option 2 outlines.
         */

        // Retrieves NotificationCompat.Builder used to create initial Notification
        NotificationCompat.Builder notificationCompatBuilder =
                GlobalNotificationBuilder.getNotificationCompatBuilderInstance();

        // Recreate builder from persistent state if app process is killed
        if (notificationCompatBuilder == null) {
            // Note: New builder set globally in the method
            notificationCompatBuilder = recreateBuilderWithMessagingStyle();
        }

        // Since we are adding to the MessagingStyle, we need to first retrieve the
        // current MessagingStyle from the Notification itself.
        Notification notification = notificationCompatBuilder.build();
        MessagingStyle messagingStyle =
                NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(
                        notification);

        // Add new message to the MessagingStyle. Set last parameter to null for responses
        // from user.
        messagingStyle.addMessage(replyCharSequence, System.currentTimeMillis(), (Person) null);

        // Updates the Notification
        notification = notificationCompatBuilder.setStyle(messagingStyle).build();

        // Pushes out the updated Notification
        NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(getApplicationContext());
        notificationManagerCompat.notify(MainActivity.NOTIFICATION_ID, notification);
    }
}