You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android 8.0(Oreo)设备FCM推送异常问题咨询

解决Android Oreo及以上FCM推送自定义音效失效、通知不显示的问题

这个问题的核心是Android 8.0(API 26)引入了通知渠道(Notification Channel)机制——所有通知必须归属到指定渠道,否则不仅不会在状态栏显示,自定义音效这类个性化设置也会直接失效。下面是具体的解决步骤:

1. 先创建专属通知渠道

在你的Application类或者App启动的第一个Activity中,添加渠道初始化代码,确保App启动时就完成渠道注册:

private void createNotificationChannel() {
    // 仅针对Oreo及以上版本执行
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String channelId = "fcm_custom_sound_channel"; // 自定义渠道ID,要记下来后面用
        String channelName = "FCM自定义音效通知"; // 渠道名称,用户在系统设置里能看到
        String channelDesc = "用于接收带自定义音效的FCM推送通知"; // 渠道描述
        int importance = NotificationManager.IMPORTANCE_HIGH; // 高优先级,保证前台也能显示通知

        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setDescription(channelDesc);
        
        // 绑定自定义音效到渠道
        Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.custom_sound);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        channel.setSound(soundUri, audioAttributes);
        
        // 把渠道注册到系统
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

2. 修改FCM推送请求的Payload

在你的推送请求里,必须添加channel_id字段,指定刚才创建的渠道ID,否则FCM会使用默认渠道,默认渠道不会应用你的自定义音效:

{
  "to" : "fcm_token",
  "notification" : {
    "priority" : "high",
    "title": "Greetings!",
    "body" : "Hope you are enjoying the day.",
    "sound" : "custom_sound",
    "channel_id" : "fcm_custom_sound_channel" // 必须和代码里的渠道ID完全一致
  }
}

3. 几个关键注意点

  • 通知渠道一旦创建,后续修改渠道的音效、优先级等设置不会自动生效——如果要调整,要么删除旧渠道重新创建,要么让用户在系统设置里手动修改对应渠道的配置。
  • 确保custom_sound文件是Android支持的格式(比如.mp3、.wav),并且放在res/raw目录下,文件名不要带空格或特殊字符。
  • 如果你用的是FCM数据消息(而非通知消息),需要自己构建Notification对象时手动指定渠道ID,同样要遵循上述渠道设置规则。

做完这些调整后,Oreo及以上设备就能正常显示通知并播放自定义音效了,前台状态下也能在状态栏看到推送内容。

内容的提问来源于stack exchange,提问作者Litle Dev

火山引擎 最新活动