Example usage for android.app NotificationManager IMPORTANCE_HIGH

List of usage examples for android.app NotificationManager IMPORTANCE_HIGH

Introduction

In this page you can find the example usage for android.app NotificationManager IMPORTANCE_HIGH.

Prototype

int IMPORTANCE_HIGH

To view the source code for android.app NotificationManager IMPORTANCE_HIGH.

Click Source Link

Document

Higher notification importance: shows everywhere, makes noise and peeks.

Usage

From source file:com.android.tv.tuner.setup.TunerSetupActivity.java

private static void sendNotificationInternal(Context context, String contentTitle, String contentText) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(new NotificationChannel(TUNER_SET_UP_NOTIFICATION_CHANNEL_ID,
            context.getResources().getString(R.string.ut_setup_notification_channel_name),
            NotificationManager.IMPORTANCE_HIGH));
    Notification notification = new Notification.Builder(context, TUNER_SET_UP_NOTIFICATION_CHANNEL_ID)
            .setContentTitle(contentTitle).setContentText(contentText)
            .setSmallIcon(/*from  ww w.  j a  v a2s . c om*/
                    context.getResources().getIdentifier(TAG_ICON, TAG_DRAWABLE, context.getPackageName()))
            .setContentIntent(createPendingIntentForSetupActivity(context))
            .setVisibility(Notification.VISIBILITY_PUBLIC).extend(new Notification.TvExtender()).build();
    notificationManager.notify(NOTIFY_TAG, NOTIFY_ID, notification);
}

From source file:com.irccloud.android.data.collection.NotificationsList.java

@SuppressLint("NewApi")
private android.app.Notification buildNotification(String ticker, int cid, int bid, long[] eids, String title,
        String text, int count, Intent replyIntent, String network, ArrayList<Notification> messages,
        NotificationCompat.Action otherAction, Bitmap largeIcon, Bitmap wearBackground) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel c = new NotificationChannel(String.valueOf(bid), title,
                NotificationManager.IMPORTANCE_HIGH);
        c.setGroup(String.valueOf(cid));
        ((NotificationManager) IRCCloudApplication.getInstance().getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(c);
    }/* ww w.  j av  a2  s. co m*/
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext());
    int defaults = 0;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            IRCCloudApplication.getInstance().getApplicationContext(), String.valueOf(bid))
                    .setContentTitle(
                            title + ((network != null && !network.equals(title)) ? (" (" + network + ")") : ""))
                    .setContentText(Html.fromHtml(text)).setAutoCancel(true).setTicker(ticker)
                    .setWhen(eids[0] / 1000).setSmallIcon(R.drawable.ic_stat_notify).setLargeIcon(largeIcon)
                    .setColor(IRCCloudApplication.getInstance().getApplicationContext().getResources()
                            .getColor(R.color.ic_background))
                    .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                    .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                    .setPriority(hasTouchWiz() ? NotificationCompat.PRIORITY_DEFAULT
                            : NotificationCompat.PRIORITY_HIGH)
                    .setOnlyAlertOnce(false);

    if (ticker != null && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000) {
        String ringtone = prefs.getString("notify_ringtone",
                "android.resource://"
                        + IRCCloudApplication.getInstance().getApplicationContext().getPackageName() + "/"
                        + R.raw.digit);
        if (ringtone.length() > 0)
            builder.setSound(Uri.parse(ringtone));
    }

    int led_color = Integer.parseInt(prefs.getString("notify_led_color", "1"));
    if (led_color == 1) {
        defaults = android.app.Notification.DEFAULT_LIGHTS;
    } else if (led_color == 2) {
        builder.setLights(0xFF0000FF, 500, 500);
    }

    if (prefs.getBoolean("notify_vibrate", true) && ticker != null
            && (System.currentTimeMillis() - prefs.getLong("lastNotificationTime", 0)) > 2000)
        defaults |= android.app.Notification.DEFAULT_VIBRATE;
    else
        builder.setVibrate(new long[] { 0L });

    builder.setDefaults(defaults);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong("lastNotificationTime", System.currentTimeMillis());
    editor.commit();

    Intent i = new Intent();
    i.setComponent(new ComponentName(IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
            "com.irccloud.android.MainActivity"));
    i.putExtra("bid", bid);
    i.setData(Uri.parse("bid://" + bid));
    Intent dismiss = new Intent(IRCCloudApplication.getInstance().getApplicationContext().getResources()
            .getString(R.string.DISMISS_NOTIFICATION));
    dismiss.setData(Uri.parse("irccloud-dismiss://" + bid));
    dismiss.putExtra("bid", bid);
    dismiss.putExtra("eids", eids);

    PendingIntent dismissPendingIntent = PendingIntent.getBroadcast(
            IRCCloudApplication.getInstance().getApplicationContext(), 0, dismiss,
            PendingIntent.FLAG_UPDATE_CURRENT);

    builder.setContentIntent(
            PendingIntent.getActivity(IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                    PendingIntent.FLAG_UPDATE_CURRENT));
    builder.setDeleteIntent(dismissPendingIntent);

    WearableExtender wearableExtender = new WearableExtender();
    wearableExtender.setBackground(wearBackground);
    if (messages != null && messages.size() > 0) {
        StringBuilder weartext = new StringBuilder();
        String servernick = getServerNick(messages.get(0).cid);
        NotificationCompat.MessagingStyle style = new NotificationCompat.MessagingStyle(servernick);
        style.setConversationTitle(title + ((network != null) ? (" (" + network + ")") : ""));
        for (Notification n : messages) {
            if (n != null && n.message != null && n.message.length() > 0) {
                if (weartext.length() > 0)
                    weartext.append("<br/>");
                if (n.message_type.equals("buffer_me_msg")) {
                    style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000,
                            " " + ((n.nick == null) ? servernick : n.nick));
                    weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick).append("</b> ")
                            .append(n.message);
                } else {
                    style.addMessage(Html.fromHtml(n.message).toString(), n.eid / 1000, n.nick);
                    weartext.append("<b>&lt;").append((n.nick == null) ? servernick : n.nick)
                            .append("&gt;</b> ").append(n.message);
                }
            }
        }

        ArrayList<String> history = new ArrayList<>(messages.size());
        for (int j = messages.size() - 1; j >= 0; j--) {
            Notification n = messages.get(j);
            if (n != null) {
                if (n.nick == null)
                    history.add(Html.fromHtml(n.message).toString());
                else
                    break;
            }
        }
        builder.setRemoteInputHistory(history.toArray(new String[history.size()]));
        builder.setStyle(style);

        if (messages.size() > 1) {
            wearableExtender.addPage(
                    new NotificationCompat.Builder(IRCCloudApplication.getInstance().getApplicationContext())
                            .setContentText(Html.fromHtml(weartext.toString()))
                            .extend(new WearableExtender().setStartScrollBottom(true)).build());
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            weartext.setLength(0);
            int j = 0;
            for (Notification n : messages) {
                if (messages.size() - ++j < 3) {
                    if (n != null && n.message != null && n.message.length() > 0) {
                        if (weartext.length() > 0)
                            weartext.append("<br/>");
                        if (n.message_type.equals("buffer_me_msg")) {
                            weartext.append("<b> ").append((n.nick == null) ? servernick : n.nick)
                                    .append("</b> ").append(n.message);
                        } else {
                            weartext.append("<b>&lt;").append((n.nick == null) ? servernick : n.nick)
                                    .append("&gt;</b> ").append(n.message);
                        }
                    }
                }
            }

            RemoteViews bigContentView = new RemoteViews(
                    IRCCloudApplication.getInstance().getApplicationContext().getPackageName(),
                    R.layout.notification_expanded);
            bigContentView.setTextViewText(R.id.title,
                    title + (!title.equals(network) ? (" (" + network + ")") : ""));
            bigContentView.setTextViewText(R.id.text, Html.fromHtml(weartext.toString()));
            bigContentView.setImageViewBitmap(R.id.image, largeIcon);
            bigContentView.setLong(R.id.time, "setTime", eids[0] / 1000);
            if (count > 3) {
                bigContentView.setViewVisibility(R.id.more, View.VISIBLE);
                bigContentView.setTextViewText(R.id.more, "+" + (count - 3) + " more");
            } else {
                bigContentView.setViewVisibility(R.id.more, View.GONE);
            }
            if (replyIntent != null && prefs.getBoolean("notify_quickreply", true)) {
                bigContentView.setViewVisibility(R.id.actions, View.VISIBLE);
                bigContentView.setViewVisibility(R.id.action_divider, View.VISIBLE);
                i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(),
                        QuickReplyActivity.class);
                i.setData(Uri.parse("irccloud-bid://" + bid));
                i.putExtras(replyIntent);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent quickReplyIntent = PendingIntent.getActivity(
                        IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                bigContentView.setOnClickPendingIntent(R.id.action_reply, quickReplyIntent);
            }
            builder.setCustomBigContentView(bigContentView);
        }
    }

    if (replyIntent != null) {
        PendingIntent replyPendingIntent = PendingIntent.getService(
                IRCCloudApplication.getInstance().getApplicationContext(), bid + 1, replyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.addAction(new NotificationCompat.Action.Builder(0, "Reply", replyPendingIntent)
                    .setAllowGeneratedReplies(true)
                    .addRemoteInput(
                            new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build())
                    .build());
        }

        NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(
                R.drawable.ic_wearable_reply, "Reply", replyPendingIntent).setAllowGeneratedReplies(true)
                        .addRemoteInput(
                                new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build());

        NotificationCompat.Action.WearableExtender actionExtender = new NotificationCompat.Action.WearableExtender()
                .setHintLaunchesActivity(true).setHintDisplayActionInline(true);

        wearableExtender.addAction(actionBuilder.extend(actionExtender).build());

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                title + ((network != null) ? (" (" + network + ")") : ""))
                        .setReadPendingIntent(dismissPendingIntent).setReplyAction(replyPendingIntent,
                                new RemoteInput.Builder("extra_reply").setLabel("Reply to " + title).build());

        if (messages != null) {
            for (Notification n : messages) {
                if (n != null && n.nick != null && n.message != null && n.message.length() > 0) {
                    if (n.buffer_type.equals("conversation")) {
                        if (n.message_type.equals("buffer_me_msg"))
                            unreadConvBuilder
                                    .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString());
                        else
                            unreadConvBuilder.addMessage(Html.fromHtml(n.message).toString());
                    } else {
                        if (n.message_type.equals("buffer_me_msg"))
                            unreadConvBuilder
                                    .addMessage(" " + n.nick + " " + Html.fromHtml(n.message).toString());
                        else
                            unreadConvBuilder
                                    .addMessage(n.nick + " said: " + Html.fromHtml(n.message).toString());
                    }
                }
            }
        } else {
            unreadConvBuilder.addMessage(text);
        }
        unreadConvBuilder.setLatestTimestamp(eids[count - 1] / 1000);

        builder.extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConvBuilder.build()));
    }

    if (replyIntent != null && prefs.getBoolean("notify_quickreply", true)
            && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        i = new Intent(IRCCloudApplication.getInstance().getApplicationContext(), QuickReplyActivity.class);
        i.setData(Uri.parse("irccloud-bid://" + bid));
        i.putExtras(replyIntent);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent quickReplyIntent = PendingIntent.getActivity(
                IRCCloudApplication.getInstance().getApplicationContext(), 0, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.addAction(R.drawable.ic_action_reply, "Quick Reply", quickReplyIntent);
    }

    if (otherAction != null) {
        int drawable = 0;
        if (otherAction.getIcon() == R.drawable.ic_wearable_add)
            drawable = R.drawable.ic_action_add;
        else if (otherAction.getIcon() == R.drawable.ic_wearable_reply)
            drawable = R.drawable.ic_action_reply;
        builder.addAction(
                new NotificationCompat.Action(drawable, otherAction.getTitle(), otherAction.getActionIntent()));
        wearableExtender.addAction(otherAction);
    }

    builder.extend(wearableExtender);

    return builder.build();
}

From source file:dk.bearware.gui.MainActivity.java

@Override
public void onCmdUserTextMessage(TextMessage textmessage) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    switch (textmessage.nMsgType) {
    case TextMsgType.MSGTYPE_CHANNEL:
    case TextMsgType.MSGTYPE_BROADCAST:
        accessibilityAssistant.lockEvents();
        textmsgAdapter.notifyDataSetChanged();
        accessibilityAssistant.unlockEvents();

        // audio event
        if (sounds.get(SOUND_CHANMSG) != 0)
            audioIcons.play(sounds.get(SOUND_CHANMSG), 1.0f, 1.0f, 0, 0, 1.0f);
        // TTS event
        if (ttsWrapper != null && prefs.getBoolean("broadcast_message_checkbox", false)) {
            User sender = ttservice.getUsers().get(textmessage.nFromUserID);
            String name = Utils.getDisplayName(getBaseContext(), sender);
            ttsWrapper.speak(getString(R.string.text_tts_broadcast_message, (sender != null) ? name : ""));
        }//from   w w  w  .  j a  v  a2  s. c o m
        Log.d(TAG, "Channel message in " + this.hashCode());
        break;
    case TextMsgType.MSGTYPE_USER:
        if (sounds.get(SOUND_USERMSG) != 0)
            audioIcons.play(sounds.get(SOUND_USERMSG), 1.0f, 1.0f, 0, 0, 1.0f);

        User sender = ttservice.getUsers().get(textmessage.nFromUserID);
        String name = Utils.getDisplayName(getBaseContext(), sender);
        String senderName = (sender != null) ? name : "";
        if (ttsWrapper != null && prefs.getBoolean("personal_message_checkbox", false))
            ttsWrapper.speak(getString(R.string.text_tts_personal_message, senderName));
        Intent action = new Intent(this, TextMessageActivity.class);
        Notification.Builder notification = new Notification.Builder(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel("TT_PM", "Teamtalk incoming message",
                    NotificationManager.IMPORTANCE_HIGH);
            mChannel.enableVibration(false);
            mChannel.setVibrationPattern(null);
            mChannel.enableLights(false);
            mChannel.setSound(null, null);
            notificationManager.createNotificationChannel(mChannel);
        }
        notification.setSmallIcon(R.drawable.message)
                .setContentTitle(getString(R.string.personal_message_notification, senderName))
                .setContentText(getString(R.string.personal_message_notification_hint))
                .setContentIntent(PendingIntent.getActivity(this, textmessage.nFromUserID,
                        action.putExtra(TextMessageActivity.EXTRA_USERID, textmessage.nFromUserID), 0))
                .setAutoCancel(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notification.setChannelId("TT_PM");
        }
        notificationManager.notify(MESSAGE_NOTIFICATION_TAG, textmessage.nFromUserID, notification.build());
        break;
    case TextMsgType.MSGTYPE_CUSTOM:
    default:
        break;
    }
}

From source file:org.restcomm.android.sdk.RCDevice.java

/**
 * Method returns the Notification builder
 * For Oreo devices we can have channels with HIGH and LOW importance.
 * If highImportance is true builder will be created with HIGH priority
 * For pre Oreo devices builder without channel will be returned
 * @param highImportance true if we need HIGH channel, false if we need LOW
 * @return/*from w  ww.j  av  a 2s .com*/
 */
private NotificationCompat.Builder getNotificationBuilder(boolean highImportance) {
    NotificationCompat.Builder builder;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        if (highImportance) {
            NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL_ID, PRIMARY_CHANNEL,
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setLightColor(Color.GREEN);
            channel.enableLights(true);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            channel.enableVibration(true);
            channel.setVibrationPattern(notificationVibrationPattern);

            ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                    .createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(RCDevice.this, PRIMARY_CHANNEL_ID);
        } else {
            NotificationManager notificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = new NotificationChannel(DEFAULT_FOREGROUND_CHANNEL_ID,
                    DEFAULT_FOREGROUND_CHANNEL, NotificationManager.IMPORTANCE_LOW);
            notificationManager.createNotificationChannel(notificationChannel);

            builder = new NotificationCompat.Builder(RCDevice.this, DEFAULT_FOREGROUND_CHANNEL_ID);
        }

    } else {
        builder = new NotificationCompat.Builder(RCDevice.this);
    }

    return builder;
}