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→includePlayerIdscontent→bodyheading→titlelanguageCode→languageandroidSmallIcon→smallIconandroidLargeIcon→largeIconandroidSound→soundOSActionButton→ActionButton
内容的提问来源于stack exchange,提问作者Jacob Handaya
相关产品推荐
相关产品推荐

