简体   繁体   English

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

[英]How to enable sound for pushnotification (FCM)?

I have an Ionic app and a Mule ESB which uses Java.我有一个 Ionic 应用程序和一个使用 Java 的 Mule ESB。 When I'm sending pushnotifications from the online console I have the option to enable sound.当我从在线控制台发送推送通知时,我可以选择启用声音。 How can I achieve this in Java?我如何在 Java 中实现这一点?

I currently have this to define my push message:我目前有这个来定义我的推送消息:

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

This is working correct, sending a notification without any sound.这是正常工作,发送没有任何声音的通知。 title and body are two variables I'm using. title 和 body 是我使用的两个变量。

To add sound I have tried to do添加声音我尝试做

.putData("sound", "default")

and something in the lines of和类似的东西

.setApnsConfig(ApnsConfig.builder().setAps(Aps.builder().setSound("default").build()).build())

as well as

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

Both without any success.两者都没有成功。 How can I achieve the same sound option as with the console inside my Java?如何实现与 Java 中的控制台相同的声音选项?

Instead of deleting, perhaps someone else might have the same issue.而不是删除,也许其他人可能有同样的问题。

So I was following a tutorial, but apparently it was deprecated.所以我正在学习教程,但显然它已被弃用。 So when reading the docs I found out what I had to do.所以在阅读文档时,我发现了我必须做的事情。

To create the notification you have to do:要创建通知,您必须执行以下操作:

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

This goes in to your message .这会影响您的message To add sound for both Android and iOS you have to add the following as well:要为 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM