如何为推送通知 (FCM) 启用声音?

How to enable sound for pushnotification (FCM)?

我有一个 Ionic 应用程序和一个使用 Java 的 Mule ESB。当我从在线控制台发送推送通知时,我可以选择启用声音。我怎样才能在 Java 中做到这一点?

我目前用这个来定义我的推送消息:

    Message message = Message.builder()
            .putData("title", title)
            .putData("body", body)
            .setTopic(topic)
            .build();

这工作正常,发送通知时没有任何声音。 title 和 body 是我使用的两个变量。

添加声音我试过

.putData("sound", "default")

行中的内容
.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setSound("default").build()).build())

以及

.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().putCustomData("sound", "default").build()).build())

都没有成功。我怎样才能获得与 Java 中的控制台相同的声音选项?

与其删除,也许其他人也有同样的问题。

所以我正在学习一个教程,但显然它已被弃用。所以在阅读文档时我发现了我必须做的事情。

要创建您必须执行的通知:

Notification notification = Notification.builder()
    .setTitle("your title or variable")
    .setBody("your body or variable")
    .build();

这会进入您的 message。要为 Android 和 iOS 添加声音,您还必须添加以下内容:

// for iOS
Aps aps = Aps.builder()
    .setSound("default")
    .build();

ApnsConfig apnsConfig = ApnsConfig.builder()
            .setAps(aps)
            .build();

// for Android  
AndroidNotification androidNofi = AndroidNotification.builder()
        .setSound("default")
        .build();

AndroidConfig androidConfig = AndroidConfig.builder()
        .setNotification(androidNofi)
        .build();

// Building the complete message to send out
Message message = Message.builder()
        .setNotification(notification)
        .setApnsConfig(apnsConfig)
        .setAndroidConfig(androidConfig)                
        .putData("page", page)
        .setTopic(topic)
        .build();