react-nativereact-native-onesignal

How to add header in new 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"]}

I tried as below but error as given above

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()
      })

I am getting the error : Error: {"errors": ["Please include a case-sensitive header of Authorization: Basic with a valid REST API key."], "reference": ["https://documentation.onesignal.com/docs/accounts-and-keys#section-keys-ids"]}

Few months i tried to send notification with fetch with contenttype and Auth header


Solution

  • FOR ANY ONE HAS THIS ERROR

     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));