简体   繁体   English

react-native-firebase自定义声音在android 8,android 9上不起作用

[英]react-native-firebase Custom sound not working on android 8, android 9

I am working on a custom sound notification features. 我正在开发自定义声音通知功能。 Custom sound is working properly on iOS and Android below version 8. Notifications also arrive in all versions of Android. 自定义声音在版本8以下的iOS和Android上正常运行。通知也到达所有版本的Android中。 But, the custom sound is not working on android 8 and 9. I have also created a channel Id. 但是,自定义声音在android 8和9上不起作用。我还创建了一个通道ID。

Below I have shared my code. 下面,我分享了我的代码。 can anyone please help me? 谁能帮帮我吗? Thanks in advance. 提前致谢。

  async checkPermission() {
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
        this.getToken();
      } else {
        this.requestPermission();
     }
    let channel = new firebase.notifications.Android.Channel(
      "channelId",
      "Channel Name",
      firebase.notifications.Android.Importance.Max
    ).setDescription("A natural description of the channel");
    firebase.notifications().android.createChannel(channel);


    firebase
      .notifications()
      .getInitialNotification()
      .then(notificationOpen => {
        if (notificationOpen) {
          const action = notificationOpen.action;
          const notification = notificationOpen.notification;
        }
      });

    // the listener returns a function you can use to unsubscribe
    this.unsubscribeFromNotificationListener = firebase
      .notifications()
      .onNotification(notification => {

        if (Platform.OS === "android") {
          const channel = new firebase.notifications.Android.Channel(
            "channelId",
            "Channel Name",
            firebase.notifications.Android.Importance.Max
          )
            .setDescription("A natural description of the channel")
            .setSound(
              notification.data.sound ? notification.data.sound : "default"
            );

          firebase.notifications().android.createChannel(channel);

          const localNotification = new firebase.notifications.Notification({
            sound: notification.data.sound
              ? notification.data.sound
              : "default",
            show_in_foreground: true
          })
            .setNotificationId(notification.notificationId)
            .setTitle(notification.title)
            .setSubtitle(notification.subtitle)
            .setBody(notification.body)
            .setData(notification.data)
            .setSound(
              notification.data.sound ? notification.data.sound : "default"
            )
            .android.setSmallIcon("notification_icon_black")
            .android.setChannelId("channelId") 
            .android.setAutoCancel(true)
            .android.setVibrate(1000)
            .android.setColor("#000000") // you can set a color here
            .android.setGroup(notification.notificationId)
            .android.setPriority(firebase.notifications.Android.Priority.High);

          firebase
            .notifications()
            .displayNotification(localNotification)
            .catch(err => console.error(err));
        } else if (Platform.OS === "ios") {
          const localNotification = new firebase.notifications.Notification()
            .setNotificationId(notification.notificationId)
            .setTitle(notification.title)
            .setSound(
              notification.data.sound ? notification.data.sound : "default"
            )
            .setSubtitle(notification.subtitle)
            .setBody(notification.body)
            .setData(notification.data)
            .ios.setBadge(notification.ios.badge);

          firebase
            .notifications()
            .displayNotification(localNotification)
            .catch(err => console.error(err));
        }

      });

    const notificationOpen = await firebase
      .notifications()
      .getInitialNotification();
    if (notificationOpen) {
      const action = notificationOpen.action;
      const notification = notificationOpen.notification;
      if (notification.data) {
        //handle data
      }
    }

    this.notificationOpenedListener = firebase
      .notifications()
      .onNotificationOpened(notificationOpen => {

        const notification = notificationOpen.notification;
        if (notification.data) {
          //handle data
        }
      });
  }



 async requestPermission() {
    try {
      await firebase.messaging().requestPermission();
      // User has authorised
      this.getToken();
    } catch (error) {
      // User has rejected permissions
      console.log("permission rejected");
    }
  }

  async getToken() {
    let fcmToken = await firebase.messaging().getToken();
    if (fcmToken) {
      console.log("fcm token===>", fcmToken);
    }
  }

Give this a try. 试试看。

First of all place your sound file in res/raw folder and also include setSound() in the notification channel , 首先,将您的声音文件放置在res / raw文件夹中,并在通知通道中包含setSound(),

Second point, thanks to @PatelDhara also make sure to remove your app from your device, so that new channel configuration will work properly. 第二点,感谢@PatelDhara,还要确保从设备中删除您的应用程序,以便新的频道配置能够正常工作。

Then put below line of code, it is working fine for me. 然后放在代码行下面,对我来说很好。

First create the channel. 首先创建频道。

const channel = new firebase.notifications.Android.Channel(name, Description, firebase.notifications.Android.Importance.High)
.setDescription(ChannelName)
.setSound(default.mp3) //Set audio here
...
firebase.notifications().android.createChannel(channel);

Then: 然后:

const notification = new firebase.notifications.Notification()
.setNotificationId(id)
.setTitle(title)
.setSound(channel.sound); //Get sound from channel and set in notification builder

notification
.android.setChannelId(channel.channelId)
firebase.notifications().displayNotification(notification);

Also make sure that whenever you get payload, your audio file name located in your res/raw folder and sound value you are getting in payload should be same. 还要确保无论何时获得有效载荷,位于res / raw文件夹中的音频文件名和要获得有效载荷的声音值都应该相同。

Like this: 像这样:

{
    "to" : "DEVICE-TOKEN",

    "notification" : {
      "body"  : "NOTIFICATION BODY",
      "title" : "NOTIFICATION TITILE",
      "sound" : "default"
    }
  }

Note : In react native there was problems with Android O in versions before 0.56.0(react native version), So try to upgrade your version. 注意:在React native中,Android O在0.56.0之前的版本(react native版本)中存在问题,因此请尝试升级您的版本。

After spending the whole day, I have found the solution. 花了一整天后,我找到了解决方案。 I have just cleared app data and restart my phone and it's working fine. 我刚刚清除了应用数据,然后重新启动了手机,一切正常。

There is an issue because I have used custom sound on a particular type of notification. 存在一个问题,因为我在特定类型的通知上使用了自定义声音。

I have also passed channel Id in the payload, to get a different sound based on the type of notification when the app is a background and kill mode. 我还在有效负载中传递了通道ID,以在应用程序为后台和终止模式时根据通知的类型获得不同的声音。

{
    "to" : "DEVICE_TOKEN",

    "notification" : {
      "body"  : "NOTIFICATION BODY",
      "title" : "NOTIFICATION TITILE",
      "sound" : "default", //change your sound based on your requirement
      "android_channel_id":"CHANEEL_NAME", 
    }
  }

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

相关问题 React-native-firebase 后台通知(Android) - React-native-firebase notification in background (Android) 使用 react-native-firebase 时添加对 Android 通知的回复 - Add replies to Android notifications while using react-native-firebase Android发行版上的react-native-firebase NullPointerException - react-native-firebase NullPointerException on android release build 无法在 Android 中设置 @react-native-firebase/messaging - Can't setup @react-native-firebase/messaging in Android android设置中的react-native-firebase依赖问题 - react-native-firebase dependency issue in android setup 安装react-native-firebase后,React Native Android应用程序崩溃 - React Native Android app crashes after installing react-native-firebase React Native - 添加 react-native-firebase 后 android 不会编译 - React Native - android wont compile after adding react-native-firebase 为什么从 firebase 控制台发送的通知能够绕过 android 后台任务限制? - 反应原生火力基地 - - Why notifications sent from firebase console are able to bypass android background task limitation? - react-native-firebase - Android Firebase 推送通知自定义声音不起作用 - Android Firebase push notification custom sound is not working react-native-firebase-收到推送通知后应用崩溃(Android) - react-native-firebase - App Crash after Push Notification Received (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM