You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic 6 Capacitor iOS端FCM通知自定义/默认音效配置问题

解决Ionic 6 Capacitor iOS端FCM默认通知音效不播放问题

以下是针对iOS端FCM通知音效失效的关键排查和解决步骤:

1. 确保通知权限包含声音授权

请求推送权限时,必须明确指定需要声音权限:

import { PushNotifications } from '@capacitor/push-notifications';

async function requestPushPermissions() {
  const result = await PushNotifications.requestPermissions({
    sound: true,
    alert: true,
    badge: true
  });
  if (result.receive === 'granted') {
    await PushNotifications.register();
  }
}

同时检查iOS系统设置中,你的App通知权限是否开启了声音选项。

2. 修正FCM Java SDK的消息配置

iOS依赖APNs处理通知,仅设置FCM的setSound("default")可能不生效,需单独配置APNs的payload:

import com.google.firebase.messaging.ApnsConfig;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.google.firebase.messaging.Aps;

// 构建APNs专属配置
ApnsConfig apnsConfig = ApnsConfig.builder()
    .putHeader("apns-priority", "10")
    .setAps(Aps.builder()
        .setSound("default") // 明确指定iOS默认音效
        .build())
    .build();

// 构建完整FCM消息
Message message = Message.builder()
    .setToken(deviceToken)
    .setNotification(Notification.builder()
        .setTitle("通知标题")
        .setBody("通知内容")
        .build())
    .setApnsConfig(apnsConfig)
    .build();

// 发送消息
String response = FirebaseMessaging.getInstance().send(message);

注意:setDefaultSound(true)需配合APNs配置使用,避免出现冲突设置。

3. 检查Xcode项目配置

  • 打开iOS项目的Info.plist,确认已添加UIBackgroundModes键,且包含remote-notification值,保证App能在后台接收通知。
  • 若项目使用了通知服务扩展(Notification Service Extension),检查扩展代码是否未篡改或移除通知的sound字段。

4. 真机测试与前台通知处理

  • iOS模拟器不支持播放通知音效,必须使用真机测试。
  • App处于前台时,iOS默认不会自动播放音效,需在监听通知接收事件时手动处理:
import { PushNotifications } from '@capacitor/push-notifications';
import { Audio } from '@capacitor-community/audio';

PushNotifications.addListener('pushNotificationReceived', async (notification) => {
  if (notification.foreground) {
    await Audio.play({
      assetId: 'default',
      volume: 1.0
    });
  }
});

(需先安装@capacitor-community/audio插件实现音频播放)

内容的提问来源于stack exchange,提问作者Manuel Guillen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 12:12:20