Android 8 notifications setSound not working

前端 未结 2 2031
[愿得一人]
[愿得一人] 2021-02-12 05:24

I have the following code but everytime I just hear the default android sound.

        // create  channel
        NotificationChannel channel = new Notification         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-12 06:11

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                Notification.Builder notificationBuilder =
                        new Notification.Builder(MyApplication.getInstance().getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setContentTitle(pTitle)
                                .setContentText(messageBody)
                                .setAutoCancel(true)
                                //.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                                //.setSound(defaultSoundUri)
                                .setContentIntent(pendingIntent);
    
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);
                // Configure the notification channel.
                AudioAttributes att = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                        .build();
                notificationChannel.setSound(defaultSoundUri,att);
                notificationChannel.setDescription(messageBody);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
                notificationChannel.enableVibration(true);
                notificationManager.createNotificationChannel(notificationChannel);
                if (imageThumbnail != null) {
                    notificationBuilder.setStyle(new Notification.BigPictureStyle()
                            .bigPicture(imageThumbnail).setSummaryText(messageBody));
                }
                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    
            } else {
                NotificationCompat.Builder notificationBuilder =
                        new NotificationCompat.Builder(MyApplication.getInstance().getApplicationContext())
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setContentTitle(pTitle)
                                .setContentText(messageBody)
                                .setAutoCancel(true)
                                .setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
                                .setSound(defaultSoundUri)
                                .setContentIntent(pendingIntent);
                if (imageThumbnail != null) {
                    notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(imageThumbnail).setSummaryText(messageBody));
                }
                notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    
            }
    

提交回复
热议问题