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

问题描述 投票:0回答:1

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

flutter push-notification onesignal
1个回答
23
投票

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

  Future<void> initOneSignal(BuildContext context) async {
    /// Set App Id.
    await OneSignal.shared.setAppId(SahityaOneSignalCollection.appID);

    /// Get the Onesignal userId and update that into the firebase.
    /// So, that it can be used to send Notifications to users later.̥
    final status = await OneSignal.shared.getDeviceState();
    final String? osUserID = status?.userId;
    // We will update this once he logged in and goes to dashboard.
    ////updateUserProfile(osUserID);
    // Store it into shared prefs, So that later we can use it.
    Preferences.setOnesignalUserId(osUserID);

    // 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
    await OneSignal.shared.promptUserForPushNotificationPermission(
      fallbackToSettings: true,
    );

    /// Calls when foreground notification arrives.
    OneSignal.shared.setNotificationWillShowInForegroundHandler(
      handleForegroundNotifications,
    );

    /// Calls when the notification opens the app.
    OneSignal.shared.setNotificationOpenedHandler(handleBackgroundNotification);
  }
© www.soinside.com 2019 - 2024. All rights reserved.