Пользовательское уведомление не работает на Oreo - почему? - PullRequest
0 голосов
/ 23 ноября 2018

так что у меня уже давно есть эта проблема.Мое уведомление вообще не издает никаких звуков, я попытался программно изменить его на звук по умолчанию, и он не работает.В настоящее время я использую эмулятор Android в Android Studio (Pixel 2XL Oreo).Ниже приведен код, я инициировал канал и установил звук в канале, и он не работает.

    // Get the uri of the sound
    Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + mContext.getPackageName() + "/" + R.raw.kepingalertupsound);

    // Get the Notification Manager using System Service for creating notification
    NotificationManager notificationManager = (NotificationManager)
            mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    // Create a notification channel to initiate notification
    // Use High importance to pop-up display
    // Check SDK version
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        // Creating Audio Attribute
        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        NotificationChannel mChannel = new NotificationChannel(
                KEPING_NOTIFICATION_CHANNEL_ID,
                mContext.getString(R.string.alert_notification_channel_name),
                NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel
        mChannel.setDescription(notificationResult);
        mChannel.enableLights(true);
        mChannel.enableVibration(true);
        mChannel.setSound(sound, attributes);

        if(notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
        }
    }

    // Build the notification here
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(mContext, KEPING_NOTIFICATION_CHANNEL_ID)
                    .setColor(ContextCompat.getColor(mContext, R.color.colorPrimaryDark))
                    .setSmallIcon(R.drawable.ic_bitcoin_color)
                    .setLargeIcon(largeIcon(mContext))
                    .setContentTitle(mContext.getString(R.string.notification_alert_title))
                    .setContentText(notificationResult)
                    .setContentIntent(contentIntent(mContext))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setAutoCancel(true);

    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
    } else {
        notificationBuilder.setChannelId(KEPING_NOTIFICATION_CHANNEL_ID);
    }

    if(notificationManager != null) {
        // Start the notification
        notificationManager.notify(ALERT_NOTIFICATION_ID, notificationBuilder.build());
    }

    // Unregister Content Observer
    mContext.getContentResolver().unregisterContentObserver(mContentObserver);
...