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

Flutter中iOS应用关闭时如何取消FCM来电通知?

iOS端FCM来电通知删除方案

1. 利用APNS折叠ID替换旧通知

iOS没有Android的tag参数,但可以通过apns-collapse-id实现类似效果——发送通知时指定相同的折叠ID,新通知会直接替换旧的,相当于自动删除原通知。

发送FCM payload时,在apns.headers中添加apns-collapse-id,建议用通话ID或用户唯一标识作为值:

{
  "to": "目标设备Token",
  "notification": {
    "title": "来电提醒",
    "body": "张三正在呼叫你"
  },
  "apns": {
    "payload": {
      "aps": {
        "sound": "custom_call_sound.wav",
        "category": "INCOMING_CALL"
      }
    },
    "headers": {
      "apns-collapse-id": "call_12345" // 用通话ID作为折叠标识
    }
  },
  "data": {
    "call_id": "12345"
  }
}

当通话发起/用户查看后,发送一个状态更新的通知(甚至空内容),使用同一个apns-collapse-id,iOS会自动替换掉原来的来电通知。

2. App内主动删除指定通知

如果需要在通话接通、用户忽略等场景主动删除通知,可通过iOS原生的UNUserNotificationCenter API实现,配合Flutter的MethodChannel调用:

原生iOS Swift代码:

import UserNotifications

class NotificationManager: NSObject, UNUserNotificationCenterDelegate {
    static let shared = NotificationManager()
    
    func removeCallNotification(withCallId callId: String) {
        let center = UNUserNotificationCenter.current()
        // 传入对应通话ID的通知标识符
        center.removeDeliveredNotifications(withIdentifiers: ["call_\(callId)"])
    }
}

Flutter端调用:

通过MethodChannel触发原生方法:

const MethodChannel _channel = MethodChannel('com.yourapp/notification');

Future<void> removeIncomingCallNotification(String callId) async {
  try {
    await _channel.invokeMethod('removeCallNotification', {'callId': callId});
  } catch (e) {
    print('删除通知失败: $e');
  }
}

3. 用户点击通知时自动删除

当用户点击来电通知进入App时,在原生的通知响应回调中删除该通知:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // 删除当前触发的通知
    center.removeDeliveredNotifications(withIdentifiers: [response.notification.request.identifier])
    // 处理通话跳转逻辑
    handleIncomingCall(response.notification.request.content.userInfo)
    completionHandler()
}

内容的提问来源于stack exchange,提问作者Yiğit ARİK

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 21:23:29