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

Flutter中One Signal真机无法推送通知问题排查

OneSignal结合External User ID推送通知真机报错问题

模拟器中可正常运行,但在真机上控制台返回Prompted But Never Answered错误,提示需移除其他通知SDK或请求权限代码,推测与Firebase FCM有关,但无法找到对应的权限请求代码。

pubspec.yaml依赖

dependencies:
  flutter:
    sdk: flutter
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.5
  font_awesome_flutter: ^10.4.0  
  easy_localization: ^3.0.2
  auto_size_text: ^3.0.0
  flutter_slidable: ^3.0.0
  ticket_widget: ^0.0.1
  qr_flutter: ^4.1.0
  flutter_easyloading: ^3.0.5
  grouped_list: ^5.1.2
  appinio_swiper: ^2.0.1
  table_calendar: ^3.0.9
  flutter_rating_stars: ^1.0.3+4
  google_fonts: ^4.0.3
  glassmorphism: ^3.0.0
  sprung: ^3.0.1
  image_picker: ^1.0.1 
  url_launcher: ^6.1.11
  dio: ^5.3.2
  flutter_session_manager: ^1.0.3
  convert: ^3.1.1
  map_launcher: ^2.5.0+1 
  flutter_inappwebview: ^5.7.2+3
  mobile_scanner: ^3.4.1
  firebase_core: ^2.14.0
  google_sign_in: ^6.1.4
  sign_in_with_apple: ^5.0.0 
  flutter_launcher_icons: ^0.13.1
  onesignal_flutter: ^5.0.0
  random_string_generator: ^2.0.0 
  flutter_holo_date_picker: ^1.1.0
  map_location_picker: ^1.2.1
  provider: ^6.0.5
  image_network: ^2.5.4+1

OneSignal初始化代码

WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
OneSignal.Debug.getDeviceState().then((value) {});
OneSignal.Notifications.requestPermission(true);
try {
  OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
} catch (e) {
  print(e);
}
if (Platform.isAndroid) {
  OneSignal.initialize(androidKey);
} else {
  OneSignal.initialize(iosKey);
}

Android端崩溃信息

Found constructor: public com.onesignal.core.internal.operations.impl.OperationRepo(java.util.List,com.onesignal.core.internal.operations.impl.OperationModelStore,com.onesignal.core.internal.config.ConfigModelStore,com.onesignal.core.internal.time.ITime)

推送实现代码

try {
  var url = Uri.parse("https://onesignal.com/api/v1/notifications");
  var client = http.Client();
  var headers = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic $auth",
  };

  var body = {
    "app_id": iosId,
    "contents": {"en": message},//if its "tr" it doesnt work
    "include_external_user_ids": [externalUserId],
    "android_sound": "sahwah",
    "ios_sound": "sahwah.wav",
    "priority": "HIGH"
  };
  var response =
      await client.post(url, headers: headers, body: json.encode(body));
  if (response.statusCode == 200) {
    print("Notification sent successfully to $externalUserId");
  } else {
    print("Error sending notification: ${response}");
  }
  print("gönder 1");
} catch (e) {
  
}

内容的提问来源于stack exchange,提问作者Hüseyin Sezen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 00:37:44