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

Posted

技术标签:

【中文标题】当应用程序未在 React-Native firebase 中运行时如何设置通知声音【英文标题】:How can I set notification sound when app is not running in React-Native firebase 【发布时间】:2019-02-12 01:50:19 【问题描述】:

我正在尝试在应用未在 ios 中运行时发出通知。

这是我的代码。

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

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

  );

当应用程序处于后台或前台时(也许我可以说“应用程序正在运行”),通知响起。

我应该使用其他监听器吗? 还是我需要添加自己的声音文件才能响铃?

【问题讨论】:

【参考方案1】:

最后,我知道该怎么做。 此问题与 react-native-firebase 无关,与我的服务器有关。

当服务器向应用发布消息时,服务器可以设置声音、徽章等多个选项。

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

如果您将声音部分设置为“默认”,即使您的应用处于后台,您也可以在收到通知时获得默认通知声音。

希望我的回答可以帮助像我这样的 react-native-firebase 初学者。

【讨论】:

你好@newbeeep,我如何在android上处理它?如果我想添加自己的声音怎么办?【参考方案2】:

任何人对 RNFIREBASE 和 REACT NATIVE 有问题,请尝试通过 Firebase ADMIN SDK 发送您的通知。这肯定会工作

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

【讨论】:

以上是关于当应用程序未在 React-Native firebase 中运行时如何设置通知声音的主要内容,如果未能解决你的问题,请参考以下文章

react-native IOS应用程序未在模拟器中显示资产

react-native 未在 Android 更新后运行

使用 Janus-gateway 的远程视频流未在 react-native 中显示

JavaScript Array flat 函数未在 React native 中定义

羽毛笔编辑器未在 React 中呈现

图像未在 Testflight 中显示