0

PHP를 사용하여 푸시 알림을 보내는 것에 대해 알아 내려고하고 있습니다. 문제는 브라우저에서 서비스 직원을 등록하고 PHP에서 직접 알림을 보내는 방법입니다.PHP가 포함 된 Google 클라우드 메시징 (GCM)

{"multicast_id":7003820144951503575,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]} 

내가 것 같아요 :

나는이 스크립트를 실행하기 위해 노력하고있어
<?php 
     define('API_ACCESS_KEY', 'SOME KEY'); 
     $registrationIds = array('fK_LVMivilI:APA91bHCXawEoNXc0Avwk4F6iYsshn4mmWX6jysrg8gC9PGA6_AlqWtr1HXhIMonCCUj8syOlsDGTFJVu_T3aPqdMNynqy7SY5L9OBlgolu-7L2a5pwZrB7kN_bdnUPeZHJQ1HT2i2ed'); 
     $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, 
      'largeIcon' => 'large_icon', 
      'smallIcon' => 'small_icon' 
     ); 
     $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; 

, 나는 다음과 같은 오류를 받고 있어요 :

나는 백엔드 부분에 대한 코드의 샘플을 얻었다 브라우저에 서비스 근로자가 등록되어 있지 않기 때문입니다.

그래서 필요한 경우 PHP와 자바 스크립트를 사용하여 동적 콘텐츠로 알림을 보내려면 몇 가지 솔루션이 필요합니다.

모든 조언이 제공됩니다.

+0

등록 ID를 확인하십시오. 맞습니까? –

답변

1

Push Messaging을 구현할 때 서비스 작업자를 등록하는 방법에 대해서는 documentation을 확인하십시오. 여기에는 웹용 푸시 메시지를 구현하는 서비스 종사자의 의존성이 있다고 명시되어 있습니다. 그 이유는 푸시 메시지가 수신되면 브라우저가 열려있는 페이지없이 백그라운드에서 실행되는 서비스 작업자를 시작하고 이벤트를 전달하여 해당 푸시 메시지 처리 방법을 결정할 수 있기 때문입니다.

이것은 이에 대한 샘플 코드입니다. 서비스 노동자에 대한 자세한 내용은

var isPushEnabled = false; 

… 

window.addEventListener('load', function() { 
var pushButton = document.querySelector('.js-push-button'); 
pushButton.addEventListener('click', function() { 
if (isPushEnabled) { 
unsubscribe(); 
} else { 
subscribe(); 
} 
}); 

// Check that service workers are supported, if so, progressively 
// enhance and add push messaging support, otherwise continue without it. 
if ('serviceWorker' in navigator) { 
navigator.serviceWorker.register('/service-worker.js') 
.then(initialiseState); 
} else { 
console.warn('Service workers aren\'t supported in this browser.'); 
} 
}); 

, 확인이 튜토리얼 :

+0

나를 위해 주요 문제는 내 PHP 백 엔드에서 데이터를 보내고 서비스 작업자 알림을 채우는 것입니다. – Viktor

관련 문제