IT박스

Android : 온라인 푸시 알림 테스트 (Google 클라우드 메시징)

itboxs 2020. 8. 21. 07:27
반응형

Android : 온라인 푸시 알림 테스트 (Google 클라우드 메시징)


업데이트 : GCM 은 더 이상 사용되지 않습니다. FCM을 사용 하세요.

내 애플리케이션에서 Google 클라우드 메시징을 구현하고 있습니다. 서버 코드가 아직 준비되지 않았으며 일부 방화벽 제한으로 인해 내 환경에 푸시 알림을위한 테스트 서버를 배포 할 수 없습니다. 내가 찾고있는 것은 내 클라이언트 구현을 테스트하기 위해 내 장치에 테스트 알림을 보내는 온라인 서버입니다.


이 작업을 수행하는 매우 쉬운 방법을 찾았습니다.

http://phpfiddle.org/ 열기

상자에 다음 PHP 스크립트를 붙여 넣습니다. PHP 스크립트 세트 API_ACCESS_KEY에서 장치 ID를 쉼표로 구분하여 설정하십시오.

F9를 누르거나 실행을 클릭합니다.

재미있게 보내세요;)

<?php


// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );


$registrationIds = array("YOUR DEVICE IDS WILL GO HERE" );

// prep the bundle
$msg = array
(
    'message'       => 'here is a message. message',
    'title'         => 'This is a title. title',
    'subtitle'      => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'              => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
?>

참고 : Google 개발자 콘솔에서 API 액세스 키를 만드는 동안 IP 주소로 0.0.0.0/0을 사용해야합니다. (테스트 목적).

편집하다:

In case of receiving invalid Registration response from GCM server, please cross check the validity of your device token. You may check the validity of your device token using following url:

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=YOUR_DEVICE_TOKEN

Some response codes:

Following is the description of some response codes you may receive from server.

{ "message_id": "XXXX" } - success
{ "message_id": "XXXX", "registration_id": "XXXX" } - success, device registration id has been changed mainly due to app re-install
{ "error": "Unavailable" } - Server not available, resend the message
{ "error": "InvalidRegistration" } - Invalid device registration Id 
{ "error": "NotRegistered"} - Application was uninstalled from the device

POSTMAN : A google chrome extension

Use postman to send message instead of server. Postman settings are as follows :

Request Type: POST

URL: https://android.googleapis.com/gcm/send

Header
  Authorization  : key=your key //Google API KEY
  Content-Type : application/json

JSON (raw) :
{       
  "registration_ids":["yours"],
  "data": {
    "Hello" : "World"
  } 
}

on success you will get

Response :
{
  "multicast_id": 6506103988515583000,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results": [
    {
      "message_id": "0:1432811719975865%54f79db3f9fd7ecd"
    }
  ]
}

Pushwatch is a free to use online GCM and APNS push notification tester developed by myself in Django/Python as I have found myself in a similar situation while working on multiple projects. It can send both GCM and APNS notifications and also support JSON messages for extra arguments. Following are the links to the testers.

Please let me know if you have any questions or face issues using it.


Postman is a good solution and so is php fiddle. However to avoid putting in the GCM URL and the header information every time, you can also use this nifty GCM Notification Test Tool

참고URL : https://stackoverflow.com/questions/22168819/android-test-push-notification-online-google-cloud-messaging

반응형