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

标签 java android firebase firebase-cloud-messaging

我必须讨论 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/

相关文章:

Java 正则表达式不区分大小写不起作用

ubuntu - 如何将Java Hotspot Server VM设置为客户端模式?

java - 将 Google map 添加到 fragment

java - 按住按钮的同时做某事

java - 使用 Firebase 数据填充 Android 微调器

android - 如何在Flutter中将项目添加到StreamController

java - Hibernate 查询示例(来自 Spring 3)

java - 正则表达式一或两个特定字母

java - 如何在 Android 中实现多 TCP 客户端/单 TCP 服务器?

Firebase 性能网络仅显示聚合请求