【问题标题】:How to get OneSignal playerId (userId) in Flutter?如何在 Flutter 中获取 OneSignal playerId (userId)?
【发布时间】:2021-07-22 13:29:57
【问题描述】:

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

【问题讨论】:

    标签: flutter push-notification onesignal


    【解决方案1】:

    这是我启动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);
      }
    

    【讨论】:

    • 如果这有帮助,请将其标记为答案:)
    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多