Flutter中如何获取OneSignal的playerId(userId)?

Pri*_*ain 8 push-notification onesignal flutter

如何在flutter应用程序中获取用户的playerId?玩家 ID 可以在 One signal 网站中找到,但我希望在 flutter 应用程序中找到它,并希望将其存储起来以向特定用户发送通知。

Moh*_*hri 25

这是我用于启动 onesignal 的 goto 函数

\n
  Future<void> initOneSignal(BuildContext context) async {\n    /// Set App Id.\n    await OneSignal.shared.setAppId(SahityaOneSignalCollection.appID);\n\n    /// Get the Onesignal userId and update that into the firebase.\n    /// So, that it can be used to send Notifications to users later.\xcc\xa5\n    final status = await OneSignal.shared.getDeviceState();\n    final String? osUserID = status?.userId;\n    // We will update this once he logged in and goes to dashboard.\n    ////updateUserProfile(osUserID);\n    // Store it into shared prefs, So that later we can use it.\n    Preferences.setOnesignalUserId(osUserID);\n\n    // The promptForPushNotificationsWithUserResponse function will show the iOS push notification prompt. We recommend removing the following code and instead using an In-App Message to prompt for notification permission\n    await OneSignal.shared.promptUserForPushNotificationPermission(\n      fallbackToSettings: true,\n    );\n\n    /// Calls when foreground notification arrives.\n    OneSignal.shared.setNotificationWillShowInForegroundHandler(\n      handleForegroundNotifications,\n    );\n\n    /// Calls when the notification opens the app.\n    OneSignal.shared.setNotificationOpenedHandler(handleBackgroundNotification);\n  }\n
Run Code Online (Sandbox Code Playgroud)\n