【问题标题】:react-native-push-notification custom sound not working when android app is killed当android应用程序被杀死时,react-native-push-notification自定义声音不起作用
【发布时间】:2021-05-29 20:52:49
【问题描述】:

我一直致力于添加自定义声音以使用 firebase-admin 版本 ^9.2.0react-native-push-notification 版本 ^5.1.0 为 react-native 应用推送通知。

我没有升级到最新版本的react-native-push-notification 的原因是即使使用正确的通道配置,自定义声音似乎也不起作用。我们也在使用 expo,使用 7+ 版本时似乎会导致启动错误。

我有两个名为 regular.mp3mass.mp3 的 mp3 文件。发送推送通知的 firebase 函数使用公共数据对象发送消息,但也使用特定于平台的字段来发送推送通知声音:

  admin.messaging().send({
    data: {
      title,
      body,
      lat: data.Latitude.toString(),
      lng: data.Longitude.toString(),
      notificationType: data.NotificationType.toString(),
    },
    notification:{title,body},
    apns:{
      payload:{
        aps:{
          alert:{
            title,
            body,
          },
          sound: data.NotificationType === 1 ? "mass.mp3" : "regular.mp3",
        },
      },
    },
    android: {
      priority: "high",
      data: {
        sound: data.NotificationType === 1 ? "mass" : "regular",
      },
      notification: {
        sound: data.NotificationType === 1 ? "mass" : "regular",
      },
    },
    topic: topic,
  })

据我了解,android 下的 data 字段确实包含在应用程序被终止并收到通知时将添加到根级 data 对象的有效负载。该插件的来源似乎也使用了确切的 data 字段来设置通知声音(在 RNReceivedMessageHandler.java 中):

JSONObject data = getPushData(notificationData.get("data"));

        if (data != null) {
            if (!bundle.containsKey("message")) {
                bundle.putString("message", data.optString("alert", null));
            }
            if (!bundle.containsKey("title")) {
                bundle.putString("title", data.optString("title", null));
            }
            if (!bundle.containsKey("sound")) {
                bundle.putString("soundName", data.optString("sound", null));
            }
            if (!bundle.containsKey("color")) {
                bundle.putString("color", data.optString("color", null));
            }

这是我目前得到的:

  • 当应用在前台时,自定义声音效果很好
  • 当应用在后台时,自定义声音效果很好
  • 应用程序被杀死时播放默认声音

这是当前用于管理通知的代码:

在 index.ts 中:

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: function(token) {
      console.log("TOKEN:", token);
  },


  // (required) Called when a remote is received or opened, or local notification is opened
  onNotification: function(notification) {
      console.log("NOTIFICATION:", notification);

      // process the notification

      // (required) Called when a remote is received or opened, or local notification is opened
      //notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
  onAction: function(notification) {
      console.log("ACTION:", notification.action);
      console.log("NOTIFICATION:", notification);

      // process the action
  },

  // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
  onRegistrationError: function(err) {
      console.error(err.message, err);
  },

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
      alert: true,
      badge: true,
      sound: true,
  },
  popInitialNotification: true,
  requestPermissions: true,
});

在 App.js 中

  CreateIncidentPushNotification=(remoteMessage)=>{
    const {data} = remoteMessage;
    PushNotification.localNotification({
      title: data.title,
      message: data.body,
      playSound: true,
      soundName: data.notificationType === "1" ? "mass" : "regular",
    });
  }

我想知道是否有其他人对可能发生的事情有所了解。即使应用程序被终止,通知仍然可以到达我的设备,这很棒。唯一缺少的部分是声音。

【问题讨论】:

    标签: android firebase react-native push-notification react-native-push-notification


    【解决方案1】:

    好的,我终于让它工作了。我必须在清单文件中添加以下内容并注释接收者:

    
          <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                    android:value="my-channel"/>
          <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                      android:value="my channel"/>
    
          <!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
          <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                      android:value="false"/>
          <!-- Change the value to false if you don't want the creation of the default channel -->
          <meta-data  android:name="com.dieam.reactnativepushnotification.channel_create_default"
                      android:value="true "/>
          <!-- Change the resource name to your App's accent color - or any other color you want -->
          <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                      android:resource="@color/white"/> <!-- or @android:color/{name} to use a standard color -->
    
          <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
          <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
          <!-- <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
              <intent-filter>
                  <action android:name="android.intent.action.BOOT_COMPLETED" />
              </intent-filter>
          </receiver> -->
    

    然后,我不得不使用通道来让声音在前台、后台和应用程序被终止时工作。如您所见,我在清单文件中创建了一个自定义频道并激活了默认频道。我必须激活默认频道,因为我有两种需要不同声音的通知类型。使用单个频道不起作用。

    清单文件更新后,我修改了 firebase 函数(使用 firebase-admin 执行以下操作:

      admin.messaging().send({
        data: {
          title,
          body,
          lat: data.Latitude.toString(),
          lng: data.Longitude.toString(),
          notificationType: data.NotificationType.toString(),
        },
        notification:{title,body},
        apns:{
          payload:{
            aps:{
              alert:{
                title,
                body,
              },
              sound: data.NotificationType === 1 ? "mass.mp3" : "regular.mp3",
            },
          },
        },
        android: {
          priority: "high",
          data: {
            sound: data.NotificationType === 1 ? "mass" : "regular",
            channelId: data.NotificationType === 1 ? "my-channel" : "fcm_fallback_notification_channel",
          },
          notification: {
            sound: data.NotificationType === 1 ? "mass" : "regular",
            channelId: data.NotificationType === 1 ? "my-channel" : "fcm_fallback_notification_channel",
          },
        },
        topic: topic,
      })
      .then((response) => {
        console.log('Successfully sent message:', response);
      })
      .catch((error) => {
        console.log('Error sending message:', error);
      });
    

    Firebase 现在知道我正在使用的两个渠道。然后我转到应用程序代码并像这样处理本地通知:

        PushNotification.localNotification({
          title: data.title,
          message: data.body,
          playSound: true,
          soundName: data.notificationType === "1" ? "mass" : "regular",
          channelId: data.notificationType === "1" ? "my-channel" : "fcm_fallback_notification_channel"
        });
    

    我还激活了react-native-push-notification 包的onMessagesetBackgroundMessageHandler 处理程序:

            messaging().onMessage(this.sendMessage);
            messaging().setBackgroundMessageHandler(this.sendMessage);
    

    其中this.sendMessage负责调用上面提到的localNotification调用。

    顺便说一句,当应用程序处于后台时,我仍然会收到重复的通知,所以这纯粹是对声音的修复。

    更新: 删除 setBackgroundMessageHandler 删除重复项!!! :) 和平!

    【讨论】:

      猜你喜欢
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2020-08-12
      • 1970-01-01
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多