devnewb
devnewb

Reputation: 101

How to enable sound for pushnotification (FCM)?

I have an Ionic app and a Mule ESB which uses Java. When I'm sending pushnotifications from the online console I have the option to enable sound. How can I achieve this in 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.

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?

Upvotes: 2

Views: 1835

Answers (1)

devnewb
devnewb

Reputation: 101

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. To add sound for both Android and iOS you have to add the following as well:

// 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();

Upvotes: 8

Related Questions