简体   繁体   English

当应用程序未在 React-Native firebase 中运行时,如何设置通知声音

[英]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.当应用程序未在 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?还是我需要添加自己的声音文件来响铃?

Finally, I know how to do this.最后,我知道如何做到这一点。 This issue was not related to react-native-firebase, it was related to my server.这个问题与 react-native-firebase 无关,它与我的服务器有关。

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 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.我希望我的回答可以帮助像我这样的 react-native-firebase 初学者。

Anyone Has issue with RNFIREBASE & REACT NATIVE, Try to send your notification via Firebase ADMIN SDK.任何人对 RNFIREBASE 和 REACT NATIVE 有问题,请尝试通过 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;
 }); 

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

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