Example usage for android.app Notification VISIBILITY_PRIVATE

List of usage examples for android.app Notification VISIBILITY_PRIVATE

Introduction

In this page you can find the example usage for android.app Notification VISIBILITY_PRIVATE.

Prototype

int VISIBILITY_PRIVATE

To view the source code for android.app Notification VISIBILITY_PRIVATE.

Click Source Link

Document

Notification visibility: Show this notification on all lockscreens, but conceal sensitive or private information on secure lockscreens.

Usage

From source file:com.example.android.wearable.wear.wearnotifications.StandaloneMainActivity.java

private void generateInboxStyleNotification() {

    Log.d(TAG, "generateInboxStyleNotification()");

    // Main steps for building a INBOX_STYLE notification:
    //      0. Get your data
    //      1. Build the INBOX_STYLE
    //      2. Set up main Intent for notification
    //      3. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.InboxStyleEmailAppData inboxStyleEmailAppData = MockDatabase.getInboxStyleData();

    // 1. 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);/*  w ww .  j a  v  a2s . c om*/
    }

    // 2. Set up main Intent for notification
    Intent mainIntent = new Intent(this, InboxMainActivity.class);

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 3. 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.

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    // 4. Build and issue the notification
    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)
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(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).setPriority(Notification.PRIORITY_HIGH)

            // Hides content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PRIVATE)
            // 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();
}

From source file:com.example.android.wearable.wear.wearnotifications.MainActivity.java

private void generateInboxStyleNotification() {

    Log.d(TAG, "generateInboxStyleNotification()");

    // Main steps for building a INBOX_STYLE notification:
    //      0. Get your data
    //      1. Build the INBOX_STYLE
    //      2. Set up main Intent for notification
    //      3. Build and issue the notification

    // 0. Get your data (everything unique per Notification)
    MockDatabase.InboxStyleEmailAppData inboxStyleEmailAppData = MockDatabase.getInboxStyleData();

    // 1. 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);/*w  w  w .ja  v  a2  s  .com*/
    }

    // 2. Set up main Intent for notification
    Intent mainIntent = new Intent(this, InboxMainActivity.class);

    // When creating your Intent, you need to take into account the back state, i.e., what
    // happens after your Activity launches and the user presses the back button.

    // There are two options:
    //      1. Regular activity - You're starting an Activity that's part of the application's
    //      normal workflow.

    //      2. Special activity - The user only sees this Activity if it's started from a
    //      notification. In a sense, the Activity extends the notification by providing
    //      information that would be hard to display in the notification itself.

    // Even though this sample's MainActivity doesn't link to the Activity this Notification
    // launches directly, i.e., it isn't part of the normal workflow, a eamil app generally
    // always links to individual emails as part of the app flow, so we will follow option 1.

    // For an example of option 2, check out the BIG_TEXT_STYLE example.

    // For more information, check out our dev article:
    // https://developer.android.com/training/notify-user/navigation.html

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(InboxMainActivity.class);
    // Adds the Intent to the top of the stack
    stackBuilder.addNextIntent(mainIntent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 3. 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.

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    // 4. Build and issue the notification
    notificationCompatBuilder

            // INBOX_STYLE sets title and content for API 16+ (4.1 and after) when the
            // notification is expanded
            .setStyle(inboxStyle)

            // Title for API <16 (4.0 and below) devices and API 16+ (4.1 and after) when the
            // notification is collapsed
            .setContentTitle(inboxStyleEmailAppData.getContentTitle())

            // Content for API <24 (7.0 and below) devices and API 16+ (4.1 and after) when the
            // notification is collapsed
            .setContentText(inboxStyleEmailAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_person_black_48dp))
            .setContentIntent(mainPendingIntent)
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

            // SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
            // devices and all Android Wear devices. If you have more than one notification and
            // you prefer a different summary notification, set a group key and create a
            // summary notification via
            // .setGroupSummary(true)
            // .setGroup(GROUP_KEY_YOUR_NAME_HERE)

            // Sets large number at the right-hand side of the notification for API <24 devices
            .setSubText(Integer.toString(inboxStyleEmailAppData.getNumberOfNewEmails()))

            .setCategory(Notification.CATEGORY_EMAIL).setPriority(Notification.PRIORITY_HIGH)

            // Hides content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PRIVATE);

    // 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);
}

From source file:com.example.android.wearable.wear.wearnotifications.StandaloneMainActivity.java

private void generateMessagingStyleNotification() {

    Log.d(TAG, "generateMessagingStyleNotification()");

    // Main steps for building a MESSAGING_STYLE notification:
    //      0. Get your data
    //      1. Build the MESSAGING_STYLE
    //      2. Add support for Wear 1.+
    //      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();

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

    MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(
            messagingStyleCommsAppData.getReplayName())
                    // You could set a different title to appear when the messaging style
                    // is supported on device (24+) if you wish. 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);
    }//from   ww  w .  j  a va  2 s .c  o  m

    // 2. Add support for Wear 1.+

    // Since Wear 1.0 doesn't support the MESSAGING_STYLE, we use the BIG_TEXT_STYLE, so all the
    // text is visible.

    // This is basically a toString() of all the Messages above.
    String fullMessageForWearVersion1 = messagingStyleCommsAppData.getFullConversation();

    Notification chatHistoryForWearV1 = new NotificationCompat.Builder(getApplicationContext())
            .setStyle(new BigTextStyle().bigText(fullMessageForWearVersion1)).setContentTitle(contentTitle)
            .setSmallIcon(R.drawable.ic_launcher).setContentText(fullMessageForWearVersion1).build();

    // Adds page with all text to support Wear 1.+.
    NotificationCompat.WearableExtender wearableExtenderForWearVersion1 = new NotificationCompat.WearableExtender()
            .setHintContentIntentLaunchesActivity(true).addPage(chatHistoryForWearV1);

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

    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 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.

    // Create the RemoteInput specifying this key.
    String replyLabel = getString(R.string.reply_label);
    RemoteInput remoteInput = new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY).setLabel(replyLabel)
            .build();

    // Create PendingIntent for service that handles input.
    Intent replyIntent = new Intent(this, MessagingIntentService.class);
    replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
    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)
                    // Allows system to generate replies by context of conversation
                    .setAllowGeneratedReplies(true)
                    // 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 current notification), we
    // create a new Builder. Later, we update this same notification, so we need to save this
    // Builder globally (as outlined earlier).

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    // Builds and issues notification
    notificationCompatBuilder
            // MESSAGING_STYLE sets title and content for API 24+ (Wear 2.0) devices
            .setStyle(messagingStyle)
            // Title for API <24 (Wear 1.+) devices
            .setContentTitle(contentTitle)
            // Content for API <24 (Wear 1.+) devices
            .setContentText(messagingStyleCommsAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_person_black_48dp))
            .setContentIntent(mainPendingIntent)
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

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

            .addAction(replyAction).setCategory(Notification.CATEGORY_MESSAGE)
            .setPriority(Notification.PRIORITY_HIGH)

            // Hides content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PRIVATE)

            // Adds multiple pages for easy consumption on a wear device.
            .extend(wearableExtenderForWearVersion1);

    // 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 : messagingStyleCommsAppData.getParticipants()) {
        notificationCompatBuilder.addPerson(name);
    }

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

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

From source file:com.example.android.wearable.wear.wearnotifications.MainActivity.java

private void generateMessagingStyleNotification() {

    Log.d(TAG, "generateMessagingStyleNotification()");

    // Main steps for building a MESSAGING_STYLE notification:
    //      0. Get your data
    //      1. Build the MESSAGING_STYLE
    //      2. Add support for Wear 1.+
    //      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();

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

    MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(
            messagingStyleCommsAppData.getReplayName())
                    // You could set a different title to appear when the messaging style
                    // is supported on device (24+) if you wish. 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);
    }/* w ww. j  av a  2s .  co m*/

    // 2. Add support for Wear 1.+

    // Since Wear 1.0 doesn't support the MESSAGING_STYLE, we use the BIG_TEXT_STYLE, so all the
    // text is visible.

    // This is basically a toString() of all the Messages above.
    String fullMessageForWearVersion1 = messagingStyleCommsAppData.getFullConversation();

    Notification chatHistoryForWearV1 = new NotificationCompat.Builder(getApplicationContext())
            .setStyle(new BigTextStyle().bigText(fullMessageForWearVersion1)).setContentTitle(contentTitle)
            .setSmallIcon(R.drawable.ic_launcher).setContentText(fullMessageForWearVersion1).build();

    // Adds page with all text to support Wear 1.+.
    NotificationCompat.WearableExtender wearableExtenderForWearVersion1 = new NotificationCompat.WearableExtender()
            .addPage(chatHistoryForWearV1);

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

    // When creating your Intent, you need to take into account the back state, i.e., what
    // happens after your Activity launches and the user presses the back button.

    // There are two options:
    //      1. Regular activity - You're starting an Activity that's part of the application's
    //      normal workflow.

    //      2. Special activity - The user only sees this Activity if it's started from a
    //      notification. In a sense, the Activity extends the notification by providing
    //      information that would be hard to display in the notification itself.

    // Even though this sample's MainActivity doesn't link to the Activity this Notification
    // launches directly, i.e., it isn't part of the normal workflow, a chat app generally
    // always links to individual conversations as part of the app flow, so we will follow
    // option 1.

    // For an example of option 2, check out the BIG_TEXT_STYLE example.

    // For more information, check out our dev article:
    // https://developer.android.com/training/notify-user/navigation.html

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(MessagingMainActivity.class);
    // Adds the Intent to the top of the stack
    stackBuilder.addNextIntent(notifyIntent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // 4. Set up RemoteInput, so users can input (keyboard and voice) from notification

    // Note: For API <24 (M and below) we need to use an Activity, so the lock-screen present
    // the auth challenge. For API 24+ (N and above), we use a Service (could be a
    // BroadcastReceiver), so the user can input from Notification or lock-screen (they have
    // choice to allow) without leaving the notification.

    // Create the RemoteInput specifying this key
    String replyLabel = getString(R.string.reply_label);
    RemoteInput remoteInput = new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY).setLabel(replyLabel)
            .build();

    // Pending intent =
    //      API <24 (M and below): activity so the lock-screen presents the auth challenge
    //      API 24+ (N and above): this should be a Service or BroadcastReceiver
    PendingIntent replyActionPendingIntent;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Intent intent = new Intent(this, MessagingIntentService.class);
        intent.setAction(MessagingIntentService.ACTION_REPLY);
        replyActionPendingIntent = PendingIntent.getService(this, 0, intent, 0);

    } else {
        replyActionPendingIntent = mainPendingIntent;
    }

    NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_reply_white_18dp, replyLabel, replyActionPendingIntent).addRemoteInput(remoteInput)
                    // Allows system to generate replies by context of conversation
                    .setAllowGeneratedReplies(true).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).

    NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(
            getApplicationContext());

    GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);

    // Builds and issues notification
    notificationCompatBuilder
            // MESSAGING_STYLE sets title and content for API 24+ (N and above) devices
            .setStyle(messagingStyle)
            // Title for API <24 (M and below) devices
            .setContentTitle(contentTitle)
            // Content for API <24 (M and below) devices
            .setContentText(messagingStyleCommsAppData.getContentText()).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_person_black_48dp))
            .setContentIntent(mainPendingIntent)
            // Set primary color (important for Wear 2.0 Notifications)
            .setColor(getResources().getColor(R.color.colorPrimary))

            // SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
            // devices and all Android Wear devices. If you have more than one notification and
            // you prefer a different summary notification, set a group key and create a
            // summary notification via
            // .setGroupSummary(true)
            // .setGroup(GROUP_KEY_YOUR_NAME_HERE)

            // Number of new notifications for API <24 (M and below) devices
            .setSubText(Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))

            .addAction(replyAction).setCategory(Notification.CATEGORY_MESSAGE)
            .setPriority(Notification.PRIORITY_HIGH)

            // Hides content on the lock-screen
            .setVisibility(Notification.VISIBILITY_PRIVATE)

            // Adds multiple pages for easy consumption on a wear device.
            .extend(wearableExtenderForWearVersion1);

    // 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 : messagingStyleCommsAppData.getParticipants()) {
        notificationCompatBuilder.addPerson(name);
    }

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