1

Im trying to send push notifications directly from a React frontend (non react-native). Curl and the expo push notification tool works fine. Only when I send if from my React frontend from ‘localhost:3000’, I am getting this error:

Access to fetch at 'https://exp.host/--/api/v2/push/send' from origin 'http://localhost:3000' 
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present 
Failed to fetch

React Code

class Form extends React.Component {
   handleSearch(event) {
   event.preventDefault();
   const message = {
   to: "ExponentPushToken[T0ZuCoB1o4ypzCbOtu2NK9]",
   sound: 'default',
   title: 'Original Title',
   body: 'And here is the body!',
   data: { data: 'goes here' },
   };

fetch('https://exp.host/--/api/v2/push/send', {
  method: 'POST',
  headers: {
    Accept: 'application/json',
    'Accept-encoding': 'gzip, deflate',
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin':'*'
 },
  body: JSON.stringify(message),
 });

 render() {
   return (
     <div>
       <input placeholder="Send a notification" onChange={this.handleTermChange} />
     </div>
     <div>
       <a onClick={this.handleSearch}>Send Push Notification</a>
     </div>
     </div>
)}
PM_903
  • 11
  • 1
  • You can also try [this](https://forums.expo.io/t/access-control-allow-origin-blocks-access-when-sending-push-tokens-with-expo-server-sdk/5303). – Ajeet Shah May 11 '20 at 22:39
  • 1
    The last link gave me the answer. It has to be sent from a server and not client. Thank you Ajeet. – PM_903 May 11 '20 at 23:09
  • Yes. Sever configures (allows or disallows) cross origin resource sharing options. – Ajeet Shah May 11 '20 at 23:16

0 Answers0