gpt4 book ai didi

java - Android Studio 自定义声音基于主题通知 FCM

转载 作者:行者123 更新时间:2023-12-02 11:18:32 24 4
gpt4 key购买 nike

我必须讨论 FCM,它工作正常..问题是我如何设置基于主题通知自定义声音通知?例如

Topic A = hello.mp3
Topic B = merra.mp3


示例代码

   private void sendNotification(String messageBody, Bitmap image, String link) {
Intent intent = new Intent(this, FullscreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("LINK", link);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);

if (FirebaseMessaging.getInstance().subscribeToTopic("topic2"))
customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

else if (FirebaseMessaging.getInstance().subscribeToTopic("topic1")
customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/raw/sound.mp3");
)
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_launcher_dishroomlogo)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(customSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

最佳答案

您可以为每个通知使用 Assets 中的自定义声音,但我给出的是默认 RingtoneManager 的示例

将主题名称作为通知正文中的字段作为数据发送

  data  = remoteMessage.getData();
topic = data.getString(“topicName”);




Uri customSoundUri;

if(topic.equalIgnoreCase("topic1"))
customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

else if(topic.equalIgnoreCase("topic2")
customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(new MyUtils().getNotificationIcon())
.setContentTitle(messageBody)
.setColor(ContextCompat.getColor(context, R.color.orange))
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);

NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

关于java - Android Studio 自定义声音基于主题通知 FCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50089555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com