首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PushNotification setSound无法在android os 8上正常工作。

PushNotification setSound无法在android os 8上正常工作。
EN

Stack Overflow用户
提问于 2019-02-21 22:17:33
回答 2查看 1.9K关注 0票数 0

我使用react本地firebase库进行推送通知,我为两个不同的通知播放了两个不同的声音,所以我为一个通知播放了一些.mp3声音,为另一个播放了默认声音,所以问题是应用程序只播放第一个通知中的声音,而对于rest通知播放第一个播放的声音,所以我认为问题是通知信息没有更新,这是它为第一个notification.even应用程序获得的所有通知播放相同的声音。我们在通知数据中获得正确的信息,但它没有更新声音。

版本: react-native-firebase:"4.3.8“react-native:"0.56.1”

是的,我正在从firebase获取数据,下面是我的代码,用于设置通知的声音。

代码语言:javascript
复制
this.notificationListener = firebase
  .notifications()
  .onNotification((notification: Notification) => {

    const channel = new firebase.notifications.Android.Channel(
      'test-channel',
      'Test Channel',
      firebase.notifications.Android.Importance.Max
    ).setDescription('My apps test channel');

    if (notification && notification.data) {
      const data = notification.data;
      if (data && data.messageKey) {
        //here I set the sound on basis of notification data to the channel

    ...
      } 
    }

    // Create the channel
    firebase.notifications().android.createChannel(channel);
    // Process your notification as required
    notification
    .android.setChannelId('test-channel')
    .android.setSmallIcon(Images.logoSmall);


firebase.notifications()
    .displayNotification(notification);
  });
EN

回答 2

Stack Overflow用户

发布于 2019-08-01 15:47:02

1)在android中,将您的自定义声音文件添加到project_root/android/app/src/main/res/raw

2)创建通知通道

代码语言:javascript
复制
const channel = new firebase.notifications.Android.Channel('channel_name', 'channel_name', firebase.notifications.Android.Importance.High)
  .setDescription('channel_name')

3)将声音添加到通知.setSound('default')

代码语言:javascript
复制
firebase.notifications().android.createChannel(channel);

const localNotification = new firebase.notifications.Notification({
  sound: 'default',
  show_in_foreground: true,
})
  .setNotificationId(new Date().valueOf().toString())
  .setTitle(noti_payload.title)
  .setSound('default')
  .setBody(noti_payload.message)
  .setData({
    now: new Date().toISOString(),
    payload: noti_payload,
  })
  .android.setAutoCancel(true)
  .android.setBigText(noti_payload.message)
  .android.setLargeIcon('ic_launchers')
  .android.setVibrate(1000)
  .android.setColor('#74c900')
  .android.setColorized(true)
  .android.setChannelId('channel_name') // e.g. the id you chose above
  .android.setSmallIcon('ic_launchers') // create this icon in Android Studio
  .android.setPriority(firebase.notifications.Android.Priority.High);

firebase
  .notifications()
  .displayNotification(localNotification)

重要说明:-

完成上述步骤后,请卸载应用程序并删除捆绑包,因为某些时间缓存的捆绑包资产包含默认声音,并且更改不会反映出来。

每次更改声音时,您都需要再次构建包

也请检查下面的链接https://rnfirebase.io/docs/v4.0.x/notifications/reference/Notification

票数 1
EN

Stack Overflow用户

发布于 2019-02-21 22:23:22

setSound()在API26中被弃用。请改用NotificationChannel.setSound()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54809170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档