自定义通知声音问题 在安卓派中



我在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);

这对我有帮助。

最新更新