Android Push Notification unauthorized error 401

gemrey

I have created an Android app with Googles' OAuth 2.0 (from developer console) and has an API key for Android application (it use Google Maps, which works by the way).

However, my problem is that I'm using Push Notifications and it works on the android application though (I get the registration ID), but on the server it doesn't work at all.

I'm using PHP and the problem seems to be the API key which is the same as the one I've for the Android application.

So what key am I supposed to use? And how do I retrieve it?

Code:

<?php

function send_push_notification($registatoin_ids, $message) {


        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $headers = array(
            'Authorization: key=An_Api_Key',
            'Content-Type: application/json'
        );
        //print_r($headers);
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
    }?>
SaurabhJinturkar

Please check allowed IP addresses in your Google API. You can restrict API access to certain IP addresses and by default one IP address is set when you create new API project.

Remove that IP address so that any IP address (Including your local machine's server) can connect and access the API.

I have implemented similar kind of code and got 401 error. Above fix worked for me. Hope that helps you as well.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

cloud endpoints return error 401 unauthorized request

분류에서Dev

Push Notification Android

분류에서Dev

Bonsai Elasticsearch 애드온 : 401 Unauthorized Error

분류에서Dev

How to port an Android app in Blackberry With Push Notification?

분류에서Dev

Push notification to android from ASP.NET webservices

분류에서Dev

401 Unauthorized Request Discord API with OAuth

분류에서Dev

Https upload MultipartForm returns 401 unauthorized

분류에서Dev

Micropython ESP8266 Twilio 401 UNAUTHORIZED

분류에서Dev

Push notification for azure notification hub

분류에서Dev

push notification using Django

분류에서Dev

IOS, handler push notification

분류에서Dev

ServiceWorker and Push Notification Payload

분류에서Dev

JWT 토큰 오류 401 Unauthorized in .net core 3.1

분류에서Dev

401 Unauthorized when using s3 API for Swift Openstack

분류에서Dev

SOAP client basic auth: HTTP response '401: Unauthorized'

분류에서Dev

DotNetNuke Service API Authorization throwing 401 Unauthorized code

분류에서Dev

TokenResponseException을 가져 오는 중 : 401 Unauthorized

분류에서Dev

Cordova Push Notification not opening App on Android (Window already focused, ignoring focus gain of)

분류에서Dev

iOS Push Notification not received on device

분류에서Dev

How to know if push notification recieved

분류에서Dev

react-native-push-notification-Android에서 작동하지 않는 로컬 알림

분류에서Dev

Android에서 push_notification 서비스를 호출하여 Drupal 삭제 토큰

분류에서Dev

IP range of google GCM push notification server?

분류에서Dev

Intercept push notification from showing in app

분류에서Dev

iOS push notification size limit and chat application

분류에서Dev

Push notification payload is without whitespace in iOS

분류에서Dev

C2Call Push Notification

분류에서Dev

Parse.com Push Notification To Friends

분류에서Dev

Apple Push Notification on Mac for Application not on appstore

Related 관련 기사

  1. 1

    cloud endpoints return error 401 unauthorized request

  2. 2

    Push Notification Android

  3. 3

    Bonsai Elasticsearch 애드온 : 401 Unauthorized Error

  4. 4

    How to port an Android app in Blackberry With Push Notification?

  5. 5

    Push notification to android from ASP.NET webservices

  6. 6

    401 Unauthorized Request Discord API with OAuth

  7. 7

    Https upload MultipartForm returns 401 unauthorized

  8. 8

    Micropython ESP8266 Twilio 401 UNAUTHORIZED

  9. 9

    Push notification for azure notification hub

  10. 10

    push notification using Django

  11. 11

    IOS, handler push notification

  12. 12

    ServiceWorker and Push Notification Payload

  13. 13

    JWT 토큰 오류 401 Unauthorized in .net core 3.1

  14. 14

    401 Unauthorized when using s3 API for Swift Openstack

  15. 15

    SOAP client basic auth: HTTP response '401: Unauthorized'

  16. 16

    DotNetNuke Service API Authorization throwing 401 Unauthorized code

  17. 17

    TokenResponseException을 가져 오는 중 : 401 Unauthorized

  18. 18

    Cordova Push Notification not opening App on Android (Window already focused, ignoring focus gain of)

  19. 19

    iOS Push Notification not received on device

  20. 20

    How to know if push notification recieved

  21. 21

    react-native-push-notification-Android에서 작동하지 않는 로컬 알림

  22. 22

    Android에서 push_notification 서비스를 호출하여 Drupal 삭제 토큰

  23. 23

    IP range of google GCM push notification server?

  24. 24

    Intercept push notification from showing in app

  25. 25

    iOS push notification size limit and chat application

  26. 26

    Push notification payload is without whitespace in iOS

  27. 27

    C2Call Push Notification

  28. 28

    Parse.com Push Notification To Friends

  29. 29

    Apple Push Notification on Mac for Application not on appstore

뜨겁다태그

보관