GCM with PHP (Google Cloud Messaging)

后端 未结 13 1786
轮回少年
轮回少年 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:05

    I actually have this working now in a branch in my Zend_Mobile tree: https://github.com/mwillbanks/Zend_Mobile/tree/feature/gcm

    This will be released with ZF 1.12, however, it should give you some great examples on how to do this.

    Here is a quick demo on how it would work....

    setId(time());
    $message->addToken('ABCDEF0123456789');
    $message->setData(array(
        'foo' => 'bar',
        'bar' => 'foo',
    ));
    
    $gcm = new Zend_Mobile_Push_Gcm();
    $gcm->setApiKey('MYAPIKEY');
    
    $response = false;
    
    try {
        $response = $gcm->send($message);
    } catch (Zend_Mobile_Push_Exception $e) {
        // all other exceptions only require action to be sent or implementation of exponential backoff.
        die($e->getMessage());
    }
    
    // handle all errors and registration_id's
    foreach ($response->getResults() as $k => $v) {
        if ($v['registration_id']) {
            printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
        }
        if ($v['error']) {
            printf("%s had an error of: %s\r\n", $k, $v['error']);
        }
        if ($v['message_id']) {
            printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
        }
    }
    

提交回复
热议问题