简体   繁体   中英

Google cloud messaging (GCM) with PHP

I'm trying to figure out about sending push notification using PHP. The problem is, how to register service worker in browser and send notification directly from PHP.

I got sample of code for back-end part:

    <?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;

When I'm trying to run this script, I'm getting the following error:

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

I guess it's because there are no service workers registered in the browser.

So, I need some solution to send notification with dynamic content using PHP and javascript if needed.

Any advice appricated.

Check this documentation on how to register a Service Worker in implementing Push Messaging. It is stated here that there is a dependency of having a service worker to implement push messages for the web. The reason for this is that when a push message is received, the browser can start up a service worker, which runs in the background without a page being open, and dispatch an event so that you can decide how to handle that push message.

This is the sample code use for this.

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.'); 
} 
});

For more information about Service Worker, check this tutorials:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM