newbeeep
newbeeep

Reputation: 625

How can I set notification sound when app is not running in React-Native firebase

I'm trying to make notification ringing when app is not running in IOS.

Here is my code.

  this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {

    // SET SYSTEM DEFAULT SOUND!
    notification.setSound("default");
    firebase.notifications().displayNotification(notification);

  });

When app is in background or foreground ( Maybe I can say 'app is running'), notification rings well.

Should I need to use other listener? Or should I need to add my own sound file for ringing?

Upvotes: 6

Views: 10307

Answers (3)

Raj Purswani
Raj Purswani

Reputation: 44

Use data and remove notification from server payload and don't forget to pass sound param in data

'data' => [
                'title' => $title,
                'body' => $body,
                'sound' => 'sound.mp3',
                'custom_key'=> 'value'
            ]

Upvotes: 0

Joel Jerushan
Joel Jerushan

Reputation: 655

Anyone Has issue with RNFIREBASE & REACT NATIVE, Try to send your notification via Firebase ADMIN SDK. That will work for Sure

  //Notification Payload
  const payload = {
       notification: {
            title: update.title,
            body: update.body,
            icon: "default",
            badge: '1',
            sound: 'default' // notification sound 
        },
  };
  
  //sending notification via Firebase Admin SDK
  admin.messaging().sendToDevice('TOKEN_OR_TOKENS', payload)
    .then( () => { return true; }).catch(function() {
        console.log('error sendToDevice() ')
        return null;
 }); 

Upvotes: 2

newbeeep
newbeeep

Reputation: 625

Finally, I know how to do this. This issue was not related to react-native-firebase, it was related to my server.

When server post message to app, server can set several options like sound, badge.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1

If you set sound section to 'default', you can get default notification sound when notification is coming even if your app is on background.

I hope my answer can help react-native-firebase beginners like me.

Upvotes: 5

Related Questions