android - ANDROID PIE 中的自定义通知声音问题

标签 android push-notification android-9.0-pie

我在 Android Pie 中使用自定义声音通知时遇到问题。当应用程序在前台时,通知会以相同的声音通知。当应用程序处于后台或终止其通知时,声音会自动更改为默认值。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            makeNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
    }
        // [END receive_message]

    private void makeNotification(String title, String messageBody) {
        Intent intent = new Intent(this, WaiterHandlerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.notification_icon);
        Bitmap scaled = Bitmap.createScaledBitmap(b, 300, 300, true);
        Uri alarmSound = Uri.parse("android.resource://" + this.getPackageName() + "/" + R.raw.sonar_ping);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(MyFirebaseMessagingService.this)
                        .setContentTitle(getResources().getString(R.string.app_name))
                        .setPriority(NotificationCompat.PRIORITY_MAX).setContentText(title).setAutoCancel(true).setSound(alarmSound)
                        .setSubText(messageBody)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
                        .setContentIntent(pendingIntent)
                        .setColor(ContextCompat.getColor(this, R.color.colorPrimaryThemeDark));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            Random r = new Random();
            NotificationChannel channel = new NotificationChannel(r.nextInt() + "",
                    "YOUR_CHANNEL_NAME",
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setSound(alarmSound, audioAttributes);

        }
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notificationBuilder.setSmallIcon(R.drawable.notification_icon);
            notificationBuilder.setLargeIcon(scaled);
            notificationBuilder.setColor(getResources().getColor(R.color.colorPrimaryThemeDark));
        } else {
            notificationBuilder.setLargeIcon(scaled);
            notificationBuilder.setSmallIcon(R.drawable.notification_icon);
        }
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
            String channelID = "Your Channel ID";// The id of the channel.
            int importance = NotificationManager.IMPORTANCE_HIGH;
            notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            NotificationChannel mChannel = new NotificationChannel(channelID, "My_Name", importance);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            mChannel.setSound(alarmSound, audioAttributes);
            notificationManager.createNotificationChannel(mChannel);
            // Create a notification and set the notification channel.
            Notification notification = notificationBuilder
                    .setChannelId(channelID)
                    .build();
            Random r = new Random();
            notificationManager.createNotificationChannel(mChannel);
            notificationManager.notify(r.nextInt(), notification);
        } else if (notificationManager != null) {
            Random r = new Random();
           NotificationCompat.Builder notificationBuilder = getNotificationBuilder();
            notificationManager.notify(r.nextInt() /* ID of notification                 notificationBuilder.build());
        }
            Random r =new Random();
            notificationManager.notify(r.nextInt());
    }
}

最佳答案

尝试更改通知重要性。

 NotificationChannel channel = new NotificationChannel(EVENT_CHANNEL_ID,
                channelName, NotificationManager.IMPORTANCE_LOW);

这对我有帮助。

关于android - ANDROID PIE 中的自定义通知声音问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57982813/

相关文章:

android - 如何从 Flutter 的通知栏中删除所有通知?

push-notification - 如何在单击按钮时消除 uWP 中的 toast

android - Android 9 异步任务中的 java.lang.NoClassDefFoundError

没有 TLS 的 Android P : network-security-config: cleartextTrafficPermitted not possible for IP (only domain)

java - 如何在Android中过滤JsonObject中的值?

android - 去除 CardView 的圆角

ios - 如何使用FCM(iOS)获取远程通知的内容?

android - AndroidManifest.xml中的using-library标签与Gradle中的useLibrary

android - 蓝牙连接在 Android 中丢失

c# - 从 Android 调用 C# Unity 脚本