Flutter OneSignal外部用户ID

flseospp  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(122)

在Flutter中,是否有任何方法可以为基于用户的应用程序推送通知。在文档中,我必须使用external_user_id。但是在Flutter中我找不到这个功能。

await OneSignal.shared.postNotificationWithJson({
   
    "include_external_user_ids": ["123456789"],
    "contents": {"en": "test notification"}
  });

然后我尝试使用postNotificationWithJson,但它返回此错误

PlatformException (PlatformException(OneSignal, Encountered an error attempting to postNotification {"errors":["Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> or Bearer token=\"<YOUR-REST-API-KEY-HERE>\" with a valid REST API key."],"reference":["https:\/\/documentation.onesignal.com\/docs\/accounts-and-keys#section-keys-ids"]}, {reference: [https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids], errors: [Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> or Bearer token="<YOUR-REST-API-KEY-HERE>" with a valid REST API key.]}, null))
ivqmmu1c

ivqmmu1c1#

在Flutter one_signal ^3.5.1中,我没有找到这个东西。

void sendNotification(String externalUserId, String message) async {
  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 $key", //from one signal
    };

    var body = {
      "app_id": appId,
      "contents": {"en": message},
      "include_external_user_ids": [externalUserId],
    };

    var response =
        await client.post(url, headers: headers, body: json.encode(body));

    if (response.statusCode == 200) {
      print("Notification sent successfully");
    } else {
      print("Error sending notification: ${response.statusCode}");
        }
      } catch (e) {
        print(e);
      }
    }

您可以通过此方法将POST与HTML一起使用

相关问题