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

Flutter升级OneSignal至5.0.1后,OSCreateNotification类缺失如何创建通知?

OneSignal 5.0.1 Flutter 版本内创建通知的替代方案

OneSignal 5.x 版本对API做了大幅重构,OSCreateNotification类被移除,改用**CreateNotificationRequest结合NotificationContent**等新类来构建通知。以下是对应你旧代码的改写方案:

核心改动说明

  • 原OSCreateNotification拆分为CreateNotificationRequest(指定接收目标、按钮等)和NotificationContent(配置通知内容、图标、声音等)
  • OSActionButton替换为ActionButton

完整改写代码

import 'package:onesignal_flutter/onesignal_flutter.dart';

// 构建通知内容
final content = NotificationContent(
  title: title,
  body: message, // 旧版的content对应新版的body字段
  language: 'en', // 旧版的languageCode对应新版的language
  smallIcon: 'ic_stat_one_signal_default', // 旧版的androidSmallIcon
  largeIcon: 'ic_stat_one_signal_default', // 旧版的androidLargeIcon
  sound: sound, // 旧版的androidSound
  iosAttachments: {"id1": imgUrlString}, // 保留原字段用法
  // bigPicture: imgUrlString, // 如需启用大图片,直接添加该字段
  // url: url, // 如需启用跳转链接,添加该字段
  actionButtons: [
    ActionButton(text: "OK", id: "id1") // 旧版OSActionButton替换为ActionButton
  ],
);

// 构建通知请求
final request = CreateNotificationRequest(
  content: content,
  includePlayerIds: listOneSignalUserId, // 旧版的playerIds对应新版的includePlayerIds
);

// 发送通知
final response = await OneSignal.Notifications.postNotification(request);

参数映射对照表

  • playerIds → includePlayerIds
  • content → body
  • heading → title
  • languageCode → language
  • androidSmallIcon → smallIcon
  • androidLargeIcon → largeIcon
  • androidSound → sound
  • OSActionButton → ActionButton

内容的提问来源于stack exchange,提问作者Jacob Handaya

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:48:15