Example usage for android.app NotificationChannel setSound

List of usage examples for android.app NotificationChannel setSound

Introduction

In this page you can find the example usage for android.app NotificationChannel setSound.

Prototype

public void setSound(Uri sound, AudioAttributes audioAttributes) 

Source Link

Document

Sets the sound that should be played for notifications posted to this channel and its audio attributes.

Usage

From source file:com.sxt.chat.utils.NotificationHelper.java

/**
 * ?//from   w  w  w  . j  a  v  a 2  s . co m
 * <p>
 * note : notify() ? ???,?? so, Create?
 */
public NotificationHelper(Context ctx) {
    super(ctx);
    context = ctx;
    soundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notify_message);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        AudioAttributes att = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH).build();
        NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL, "Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        channel.setLightColor(Color.YELLOW);
        channel.setShowBadge(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(channel);

        NotificationChannel chanCustom = new NotificationChannel(CUSTOM_NOTIFY_CHANNEL, "Custom Layout Channel",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setSound(soundUri, att);
        chanCustom.setLightColor(Color.RED);
        chanCustom.setShowBadge(true);
        chanCustom.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(chanCustom);
    }
}

From source file:com.frostwire.android.gui.NotificationUpdateDemon.java

private void updatePermanentStatusNotification() {
    if (!ConfigurationManager.instance()
            .getBoolean(Constants.PREF_KEY_GUI_ENABLE_PERMANENT_STATUS_NOTIFICATION)) {
        return;//from  w  w w . jav  a2 s .c o m
    }

    if (notificationViews == null || notificationObject == null) {
        LOG.warn("Notification views or object are null, review your logic");
        return;
    }

    // number of uploads (seeding) and downloads
    TransferManager transferManager;

    try {
        transferManager = TransferManager.instance();
    } catch (IllegalStateException btEngineNotReadyException) {
        return;
    }

    if (transferManager != null) {
        int downloads = transferManager.getActiveDownloads();
        int uploads = transferManager.getActiveUploads();
        if (downloads == 0 && uploads == 0) {
            NotificationManager manager = (NotificationManager) mParentContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                try {
                    manager.cancel(Constants.NOTIFICATION_FROSTWIRE_STATUS);
                } catch (SecurityException ignored) {
                    // possible java.lang.SecurityException
                }
            }
            return; // quick return
        }
        //  format strings
        String sDown = UIUtils.rate2speed(transferManager.getDownloadsBandwidth() / 1024);
        String sUp = UIUtils.rate2speed(transferManager.getUploadsBandwidth() / 1024);
        // Transfers status.
        notificationViews.setTextViewText(R.id.view_permanent_status_text_downloads, downloads + " @ " + sDown);
        notificationViews.setTextViewText(R.id.view_permanent_status_text_uploads, uploads + " @ " + sUp);
        final NotificationManager notificationManager = (NotificationManager) mParentContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            try {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(
                            Constants.FROSTWIRE_NOTIFICATION_CHANNEL_ID, "FrostWire",
                            NotificationManager.IMPORTANCE_MIN);
                    channel.setSound(null, null);
                    notificationManager.createNotificationChannel(channel);
                }
                notificationManager.notify(Constants.NOTIFICATION_FROSTWIRE_STATUS, notificationObject);
            } catch (SecurityException ignored) {
                // possible java.lang.SecurityException
                ignored.printStackTrace();
            } catch (Throwable ignored2) {
                // possible android.os.TransactionTooLargeException
                ignored2.printStackTrace();
            }
        }
    }
}

From source file:com.frostwire.android.gui.services.EngineService.java

public void notifyDownloadFinished(String displayName, File file, String infoHash) {
    try {// w  w w .  j  av a 2 s  .co m
        if (notifiedStorage.contains(infoHash)) {
            // already notified
            return;
        } else {
            notifiedStorage.add(infoHash);
        }

        Context context = getApplicationContext();
        Intent i = new Intent(context, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        i.putExtra(Constants.EXTRA_DOWNLOAD_COMPLETE_NOTIFICATION, true);
        i.putExtra(Constants.EXTRA_DOWNLOAD_COMPLETE_PATH, file.getAbsolutePath());
        PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(context,
                Constants.FROSTWIRE_NOTIFICATION_CHANNEL_ID).setWhen(System.currentTimeMillis())
                        .setContentText(getString(R.string.download_finished))
                        .setContentTitle(getString(R.string.download_finished))
                        .setSmallIcon(getNotificationIcon()).setContentIntent(pi).build();
        notification.vibrate = ConfigurationManager.instance().vibrateOnFinishedDownload() ? VENEZUELAN_VIBE
                : null;
        notification.number = TransferManager.instance().getDownloadsToReview();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        if (manager != null) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(
                        Constants.FROSTWIRE_NOTIFICATION_CHANNEL_ID, "FrostWire",
                        NotificationManager.IMPORTANCE_MIN);
                channel.setSound(null, null);
                manager.createNotificationChannel(channel);
            }
            manager.notify(Constants.NOTIFICATION_DOWNLOAD_TRANSFER_FINISHED, notification);
        }
    } catch (Throwable e) {
        LOG.error("Error creating notification for download finished", e);
    }
}

From source file:im.vector.notifications.NotificationUtils.java

/**
 * Add a notification groups./*from  w w  w  .ja  v a2 s .  c o m*/
 *
 * @param context the context
 */
@SuppressLint("NewApi")
public static void addNotificationChannels(Context context) {
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }

    if (null == NOISY_NOTIFICATION_CHANNEL_NAME) {
        NOISY_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.notification_noisy_notifications);
    }

    if (null == SILENT_NOTIFICATION_CHANNEL_NAME) {
        SILENT_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.notification_silent_notifications);
    }

    if (null == CALL_NOTIFICATION_CHANNEL_NAME) {
        CALL_NOTIFICATION_CHANNEL_NAME = context.getString(R.string.call);
    }

    if (null == LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME) {
        LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME = context
                .getString(R.string.notification_listen_for_events);
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    // A notification channel cannot be updated :
    // it must be deleted and created with another channel id
    if ((null == NOISY_NOTIFICATION_CHANNEL_ID)) {
        List<NotificationChannel> channels = notificationManager.getNotificationChannels();

        for (NotificationChannel channel : channels) {
            if (channel.getId().startsWith(NOISY_NOTIFICATION_CHANNEL_ID_BASE)) {
                NOISY_NOTIFICATION_CHANNEL_ID = channel.getId();
            }
        }
    }

    if (null != NOISY_NOTIFICATION_CHANNEL_ID) {
        NotificationChannel channel = notificationManager.getNotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID);
        Uri notificationSound = channel.getSound();
        Uri expectedSound = PreferencesManager.getNotificationRingTone(context);

        // the notification sound has been updated
        // need to delete it, to create a new one
        // else the sound won't be updated
        if (((null == notificationSound) ^ (null == expectedSound)) || ((null != notificationSound)
                && !TextUtils.equals(notificationSound.toString(), expectedSound.toString()))) {
            notificationManager.deleteNotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID);
            NOISY_NOTIFICATION_CHANNEL_ID = null;
        }
    }

    if (null == NOISY_NOTIFICATION_CHANNEL_ID) {
        NOISY_NOTIFICATION_CHANNEL_ID = NOISY_NOTIFICATION_CHANNEL_ID_BASE + System.currentTimeMillis();

        NotificationChannel channel = new NotificationChannel(NOISY_NOTIFICATION_CHANNEL_ID,
                NOISY_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(NOISY_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(PreferencesManager.getNotificationRingTone(context), null);
        channel.enableVibration(true);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(SILENT_NOTIFICATION_CHANNEL_NAME)) {
        NotificationChannel channel = new NotificationChannel(SILENT_NOTIFICATION_CHANNEL_ID,
                SILENT_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(SILENT_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_ID)) {
        NotificationChannel channel = new NotificationChannel(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_ID,
                LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN);
        channel.setDescription(LISTEN_FOR_EVENTS_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }

    if (null == notificationManager.getNotificationChannel(CALL_NOTIFICATION_CHANNEL_ID)) {
        NotificationChannel channel = new NotificationChannel(CALL_NOTIFICATION_CHANNEL_ID,
                CALL_NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(CALL_NOTIFICATION_CHANNEL_NAME);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }
}

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 ww  .  j  av a 2 s . com*/
        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;
    }
}