API 26上的通知声音

时间:2017-09-03 01:21:57

标签: android push-notification

我有一个我在通知中使用的自定义mp3声音。它适用于API 26以下的所有设备。我也尝试在Notification Channel上设置声音,但仍无法正常工作。它播放默认声音。

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.icon_push)
            .setColor(ContextCompat.getColor(this, R.color.green))
            .setContentTitle(title)
            .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .build();
            channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(1, notification);

6 个答案:

答案 0 :(得分:33)

您可能最初使用默认声音创建了频道。一旦创建了频道,就无法更改。您需要重新安装应用程序或使用新的渠道ID创建渠道。

答案 1 :(得分:10)

我使用了RingtoneManager,它对我有用。试试thius代码:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("Notification title");
    builder.setContentText("Notification message.");
    builder.setSubText("Url link.");

    try {
        Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

答案 2 :(得分:2)

在奥利奥,您需要为此创建一个频道。 https://developer.android.com/reference/android/app/NotificationChannel.html

答案 3 :(得分:1)

默认声音会覆盖任何声音。

你需要把它放在你的代码中:

notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;

参考:

Android notifications

答案 4 :(得分:0)

除了Faxriddin的答案,您还可以通过查看importance of the notification channelare enabled notifications参数来确定何时应关闭通知声音。

NotificationChannel channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
if(notificationManager.areNotificationsEnabled() && channel.getImportance() != NotificationManager.IMPORTANCE_NONE) {
  try {
     Ringtone r = RingtoneManager.getRingtone(ctx, soundUri);
     r.play();
  } catch (Exception e) { }
}

答案 5 :(得分:0)

我一直在开发具有类似功能的应用程序。正如前面提到的PawełNadolski所说,我们每次都必须重新创建频道以更改铃声。为此,我写了助手。希望它可以帮助某人。

@RequiresApi(Build.VERSION_CODES.O)
object ChannelHelper {
    // channel id for 8.0 OS version and higher
    private const val OREO_CHANNEL_ID = "com.ar.app.notifications"
    private const val CHANNEL_ID_PREF = "com.ar.app.notifications_prefs"

    @JvmStatic
    fun createChannel(context: Context, playSound: Boolean, isVibrated: Boolean, uri: Uri) {
        // establish name and importance of channel
        val name = context.getString(R.string.main_channel_name)
        val importance = if (playSound) {
            NotificationManager.IMPORTANCE_DEFAULT
        } else {
            NotificationManager.IMPORTANCE_LOW
        }

        // create channel
        val channelId = OREO_CHANNEL_ID + UUID.randomUUID().toString()
        saveChannelId(context, channelId)

        val channel = NotificationChannel(channelId, name, importance).apply {
            enableLights(true)
            lightColor = Color.GREEN

            lockscreenVisibility = Notification.VISIBILITY_PUBLIC

            // add vibration
            enableVibration(isVibrated)
            vibrationPattern = longArrayOf(0L, 300L, 300L, 300L)

            // add sound
            val attr = AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build()

            setSound(uri, attr)
        }

        // register the channel in the system
        val notificationManager = context.getSystemService(NotificationManager::class.java)
        notificationManager.createNotificationChannel(channel)
    }

    @JvmStatic
    fun rebuildChannel(context: Context, playSound: Boolean, isVibrated: Boolean, uri: Uri) {
        val notificationManager = context.getSystemService(NOTIFICATION_SERVICE)
                as NotificationManager
        notificationManager.deleteNotificationChannel(
            getChannelId(context) ?: OREO_CHANNEL_ID
        )

        createChannel(context, playSound, isVibrated, uri)
    }

    @JvmStatic
    fun getChannel(context: Context): NotificationChannel? {
        val notificationManager = context.getSystemService(NOTIFICATION_SERVICE)
                as NotificationManager

        return notificationManager.getNotificationChannel(
                getChannelId(context) ?: OREO_CHANNEL_ID
        )
    }

    @JvmStatic
    fun isChannelAlreadyExist(context: Context) = getChannel(context) != null

    @JvmStatic
    fun getChannelId(context: Context) =
            PreferenceManager.getDefaultSharedPreferences(context)
                    .getString(CHANNEL_ID_PREF, OREO_CHANNEL_ID)

    private fun saveChannelId(context: Context, channelId: String) =
            PreferenceManager.getDefaultSharedPreferences(context)
                    .edit()
                    .putString(CHANNEL_ID_PREF, channelId)
                    .apply()
}