Ionic Google Firebase消息传递API面临CORS错误

fruv7luv  于 2022-12-16  发布在  Ionic
关注(0)|答案(1)|浏览(99)

大家好,我正在尝试订阅Firebase云消息通道,并通过capacitor/ioni应用程序使用PWA提供令牌。但当我发布www文件夹时,我遇到了CORS问题,而不是在localhost上工作。这是我在.ts文件中使用的代码

this.devices = response;
        FirebaseMessaging.requestPermissions().then(result => {
          if(result.receive === 'granted')
          {
            FirebaseMessaging.getToken(
              {
                vapidKey: 'my-vapid-key',
              }
            ).then( result => {
              const token = result.token;
              this.devices.forEach(i => {
                let topic = i.serial
                fetch('https://iid.googleapis.com/iid/v1/'+ token +'/rel/topics/'+ topic, {
                  method: 'POST',
                  headers: new Headers({
                    'Access-Control-Allow-Origin': '*',
                    "Access-Control-Allow-Methods": "DELETE, POST, GET, OPTIONS",
                    "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",  
                    'Authorization': 'key=my-key'
                  })
                }).then(response => {
                  alert('Fatto')
                  if (response.status < 200 || response.status >= 400) {
                    throw 'Error subscribing to topic: '+response.status + ' - ' + response.text();
                  }
                  console.log('Subscribed to "'+topic+'"');
                }).catch(error => {
                  console.error(error);
                })
                
              })
               
              this.addReceivedListener();

我面对的错误是:来自源“https://www.example.com”的“https://iid.googleapis.com/iid/v1/xxxxxxxxtokeeen/rel/topics/mytopic mysite.site.com "已被CORS策略阻止:对印前检查请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。如果不透明响应满足您的需要,请将请求的模式设置为“no-cors”以在禁用CORS的情况下提取资源。”

ohfgkhjo

ohfgkhjo1#

另一端没有CORS,因此我们需要禁用它并使其正常工作

相关问题