GCM with PHP (Google Cloud Messaging)

后端 未结 13 1451
轮回少年
轮回少年 2020-11-22 04:48

Update: GCM is deprecated, use FCM

How can I integrate the new Google Cloud Messaging in a PHP backend?

13条回答
  •  别跟我提以往
    2020-11-22 05:08

    You can use this PHP library available on packagist:

    https://github.com/CoreProc/gcm-php

    After installing it you can do this:

    $gcmClient = new GcmClient('your-gcm-api-key-here');
    
    $message = new Message($gcmClient);
    
    $message->addRegistrationId('xxxxxxxxxx');
    $message->setData([
        'title' => 'Sample Push Notification',
        'message' => 'This is a test push notification using Google Cloud Messaging'
    ]);
    
    try {
    
        $response = $message->send();
    
        // The send() method returns a Response object
        print_r($response);
    
    } catch (Exception $exception) {
    
        echo 'uh-oh: ' . $exception->getMessage();
    
    }
    

提交回复
热议问题