【问题标题】:Custom sound push notification does not work (Flutter)自定义声音推送通知不起作用(Flutter)
【发布时间】:2019-05-28 21:09:15
【问题描述】:
{
  "to": "XXXX",
  "notification": {
    "title": "ASAP Alert",
    "body": "Please open your app"
  },
  "data": {
    "screen": "/Nexpage1",
    "sound": "alarm",
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
  }
}

以上是我的推送通知负载。我已经在原始文件夹中插入了alarm.mp3文件,但是它仍然没有给我警报声,我也尝试了alarm.mp3,json有什么问题吗?是因为我的 dart 文件中的代码?

【问题讨论】:

    标签: push-notification dart flutter firebase-cloud-messaging


    【解决方案1】:

    阅读this 似乎应该在Android 上自动管理(如果你没有使用notification builder),但你必须指定.mp3 扩展名并将其放在notification 字段中而不是data一个..

    "sound": "alarm.mp3"
    

    iOS 在后台的行为非常不同,但您也可以通过在通知负载中设置 sound: 字段来使用自定义声音。无论如何.mp3 不是有效的 APN 通知文件格式,您还需要指定文件扩展名。

    "sound": "filename.caf"
    

    关注Apple documentation,为您的应用打造自定义声音文件。

    mp3 格式无效

    准备自定义警报声音

    本地和远程通知可以指定自定义警报声音 通知发送时播放。您可以打包音频 aiff、wav 或 caf 文件中的数据。因为他们玩的是 系统声音设施,自定义声音必须在以下之一 音频数据格式:

    • Linear PCM

    • MA4 (IMA/ADPCM)

    • µLaw

    • aLaw

    将自定义声音文件放在您的 app bundleLibrary/Sounds 应用程序容器目录的文件夹。风俗 播放时声音必须在 30 秒内。如果自定义声音是 超过该限制,将播放默认系统声音。

    您可以使用afconvert 工具来转换声音。例如,要 将 16 位线性 PCM 系统声音 Submarine.aiff 转换为 IMA4 CAF file 中的音​​频,在终端应用程序中使用以下命令:

    afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
    

    例如,将您的 mp3 文件转换为 caf 文件,您可以在终端中键入:

    afconvert -f caff -d LEI16 alarm.mp3 alarm.caf
    

    阅读此doc 以深入了解所有通用和特定通知有效负载字段。

    更新

    我已经测试了 Android 部分,我可以确认将您的 .mp3 文件放在 res/raw/ 文件夹中,声音会按记录和预期播放。

    这是我的通知负载:

    {
     "to" : "my_device_token",
     "collapse_key" : "type_a",
     "priority" : "high",
     "notification" : {
         "body" : "Test Notification body for custom sound {{datestamp}}",
         "title": "Custom sound alert.mp3",
         "sound": "alert.mp3"
     }
    }
    

    在将.mp3文件转换为.caf文件后,我也测试了iOS版本:

    afconvert -f caff -d LEI16 alert.mp3 alert.caf
    

    具有不同文件名的相同 json 有效负载有效:

    {
     "to" : "my_device_token",
     "collapse_key" : "type_a",
     "priority" : "high",
     "notification" : {
         "body" : "Test Notification body for custom sound {{datestamp}}",
         "title": "Custom sound alert.mp3",
         "sound": "alert.caf"
     }
    }
    

    记得将文件添加到您的main bundle

    如果应用程序被终止或在后台运行。

    如果您想在应用程序处于前台时显示警报并播放声音,您必须在 onMessage 事件上进行管理,就像有人已经告诉您 here 一样,或者您可以在此处使用 platform-channel使用 Android 上的 Notification.Builder 和 iOS 上的 UNNotificationCenter 构建您自己的通知(例如)。

    更新

    这个问题已经解决了。见here官方评论:

    大家好?

    作为我们路线图 (#2582) 的一部分,我们刚刚交付了一个完整的返工 firebase_messaging 插件旨在解决这个问题和许多其他问题 问题。

    如果可以,请试用开发版(请参阅migration guide 用于升级和更改),如果您有任何反馈,请加入 在讨论中here

    鉴于返工的范围,我将继续并关闭它 赞成试用最新插件的问题。

    谢谢大家?

    【讨论】:

    • 我可以知道你的设备是安卓版本吗?因为我的是奥利奥,收到通知时它仍然给我默认声音
    • 我会尝试在另一台设备上试一试
    • 您是否在notification 中设置了sound 而不是data
    • 好的!我已经打开了一个问题。我们可以跟踪它here
    • @shadowsheep 感谢您的信息。现在在最新的更新中,我需要包含扩展名吗?如果是,那么如何分别处理 iOS 上的 .caf 和 A​​ndroid 上的 .mp3?我将用户令牌保存在我的服务器上以向他们发送通知,但我没有关于用户正在使用什么设备的数据。
    【解决方案2】:

    ShadowSheep 在回答这个问题方面做得很好,但我想澄清一件事,试图让 iOS 声音正常工作。

    您必须将声音添加到 XCode(ShadowSheep 所说的将资产包含在 main bundle 中的地方)。您只需将音频文件(.caf 或上述其他支持的格式)拖放到 XCode 中的根目录(通常称为 Runner for Flutter)中:

    如果您已完成此操作并遵循上述问题/答案中描述的设置,那么您应该可以开展业务了。

    【讨论】:

      【解决方案3】:

      对我来说,我使用flutter_local_notifications 创建通知通道。

      包含此功能(可创建多个通知通道)

      Future<void> _createNotificationChannel(String id, String name,
      String description, String sound) async {
      final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
      var androidNotificationChannel = AndroidNotificationChannel(
        id,
        name,
        description,
        sound: RawResourceAndroidNotificationSound(sound),
        playSound: true,
      );
      
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
          AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(androidNotificationChannel);}
      

      在 initState 中调用函数:(这创建了 2 个通知通道)

      _createNotificationChannel("channel_id_1", "channel_name", "description", "alert");
      _createNotificationChannel("channel_id_2", "channel_name", "description", "alarm");
      

      记得将alertalarm的文件保存在res/raw中,文件格式为.mp3

      使用此有效负载:

      {
      "notification": {
          "title": "My First Notification",
          "body": "Hello, I'm push notification"
      },
      "data": {
          "title": "My First Notification"
      },
      "android": {
          "notification": {
              "channel_id": "channel_id_1"
          }
      },
      "to": "device_token"}
      

      【讨论】:

      • IOS呢?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2023-03-28
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多