firebase推送通知声音不适用于android工作室的android应用程序



我已经实现了Push通知firebase服务类,并添加到我的项目中,并通过发送通知API请求,因此通知正确接收,但通知声音仅适用于应用程序活动条件,但需要所有通知接收时间。

任何一个这种类型的条件面临和解决请建议改变。

String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
String text = remoteMessage.getNotification().getBody();
remoteMessage.getMessageType();
//       String channelId = remoteMessage.getNotification().getChannelId();
//        Log.d("channelId", "onMessageReceived: "+channelId);
final String CHANNEL_ID = "HEADS_UP_NOTIFICATION";
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Heads Up notification",
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(new long[] { 0,100,300,500,800,1000, 1000, 1000, 1000, 1000 })
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setVisibility(Notification.VISIBILITY_PUBLIC);
//  custom notification
/*   MediaPlayer mp;
mp = MediaPlayer.create(PushNotificationServices.this, R.raw.short_notification_sound);
mp.start();*/
@SuppressLint("UnspecifiedImmutableFlag") PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class),  PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
notification.setContentIntent(contentIntent);
NotificationManagerCompat.from(this).notify(1,notification.build());
super.onMessageReceived(remoteMessage);

尝试使用setSound方法而不是setDefaults

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification.Builder = Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(longArrayOf(0, 100, 300, 500, 800, 1000, 1000, 1000, 1000, 1000))
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
--> .setSound(defaultSoundUri)
.setVisibility(Notification.VISIBILITY_PUBLIC)

首先您必须卸载应用程序,然后重试。

如果这不起作用,那么尝试使用以下代码

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = CommonUtill.random();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody).setBigContentTitle(getString(R.string.app_name)))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription("desc");
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT

if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(943/* ID of notification */, notificationBuilder.build());

这是参考链接Android推送通知声音在Android 上丢失(不工作(背景和前景

最新更新