7

How to get playerId of a user inside the flutter app?. Player Id can be found in One signal website but i want that inside the flutter app and want to store it to send the notification to particular user.

Priyansh jain
  • 1,065
  • 10
  • 17

1 Answers1

24

This is my goto function for initating onesignal

  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);
  }
Mohan Sai Manthri
  • 2,808
  • 1
  • 11
  • 26
  • 1
    If this helps please mark it as answer :) – Mohan Sai Manthri Jul 23 '21 at 04:33
  • Hi I would like to ask.. I am currently install app that consist of notification in emulator and it works perfectly, and now I would like to install it in real device but the status subscribed is always false, is there a way to subscribe new device ? – wahyu Aug 12 '22 at 03:15