如何在新的 Onesignal React-native 中添加标头

问题描述

 Error: {"errors": ["Please include a case-sensitive header of Authorization: Basic <YOUR-REST-API-KEY-HERE> with a valid REST API key."],"reference": ["https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"]}

我尝试如下但错误如上

sendNotification = async (data) => {

    const { userId } = await Onesignal.getDeviceState();
    const notificationObj = {
      contents: { en: "Message Body" },include_player_ids: [userId],Authorization: "Basic APIKEY",headings: { en: 'You have new notification' },android_channel_id: 'id',template_id: 'id',buttons: [{ "id": "open_flat","text": "OPEN HOSTING","icon": "ic_menu_share" }],include_external_user_ids: ["13245-123455"],};

    const jsonString = JSON.stringify(notificationObj);

    Onesignal.postNotification(jsonString,(success) => {
      console.log("Success:",success);
    },(error) => {
      console.log("Error:",error);
    });
      };

    //Sending demo 
    useEffect(() => {
        sendNotification()
      })

我收到错误错误:{"errors": ["请包含区分大小写的 Authorization: Basic 标题和有效的 REST API 密钥。"],"reference": ["https://documentation.onesignal.com/docs/accounts- and-keys#section-keys-ids"]}

几个月来,我尝试使用带有内容类型和身份验证标头的 fetch 发送通知

解决方法

您需要将 APIKEY 替换为您的实际 API 密钥,例如您的 API 密钥是“MY_API_KEY123456”,那么标题应该是 Authorization:“Basic MY_API_KEY123456”

,

对于任何有此错误的人

 let headers = {
    'Content-Type': 'application/json; charset=utf-8',Authorization: `Basic '<API-KEY-HERE>'`,};
let endpoint = 'https://onesignal.com/api/v1/notifications';
let params = {
    method: 'POST',headers: headers,body: JSON.stringify({
        app_id: 'App Id',include_external_user_ids: [{'userid'},{'userid2'}],--> Optional
        headings: { en: 'DATA'},contents: { en: 'DATA'},buttons: [{ "id": 'id',"text": 'OPEN',"icon": "ic_baseline_open_in_new_24" }],--> OPTIONAL
        data: 'Extra data as json'

    }),};
fetch(endpoint,params).then(res => { console.log('sucess NotiButton') }).catch(error => console.log('error' + error));