iOS的苹果推送通知和使用PHP的Android的谷歌云消息?

时间:2014-10-08 10:59:54

标签: php android ios notifications push-notification

我正在寻找可以向ios设备和Android设备发送推送通知的php代码。

我个人使用easyapns进行ios推送通知,使用gcm进行android推送通知。

我希望合并这两个代码并制作单个代码,以便为这两个设备发送推送通知。

我只在php部分工作。是否有任何解决方案可以轻松实现?

2 个答案:

答案 0 :(得分:5)

在我的研究之后,我找到了一个解决方案,它适用于ios。

 public function send_notification($registration_id,$message,$ios_certificate,$badge,$tHost){
    // Provide the Host Information.
    //$tHost = 'gateway.sandbox.push.apple.com';

    $tPort = 2195;

    // Provide the Certificate and Key Data.
    //$tCert = 'certificates/meetmethere/apns-dev-cert.pem';
    $tPassphrase = '';

    //$tToken = 'efdf10632aa4e711ef8c57abf2a3cdec51e7c42811bb998c3e26de2876bac8fa';
    //$tAlert = 'this is the test message ';

    // The Badge Number for the Application Icon (integer >=0).

    $tBadge = 0;

    // Audible Notification Option.
    $tSound = 'default';

    // The content that is returned by the LiveCode "pushNotificationReceived" message.
    $tPayload = 'message';

    // Create the message content that is to be sent to the device.
    $tBody['aps'] = array (
                        'alert' => $message,
                        'badge' => $badge,
                        'sound' => $tSound,
                    );

    $tBody ['payload'] = $tPayload;
    $tBody = json_encode ($tBody);

    // Create the Socket Stream.
    $tContext = stream_context_create ();

    stream_context_set_option ($tContext, 'ssl', 'local_cert', $ios_certificate);

    // Remove this line if you would like to enter the Private Key Passphrase manually.

    stream_context_set_option ($tContext, 'ssl', 'passphrase', $tPassphrase);

    // Open the Connection to the APNS Server.

    $tSocket = stream_socket_client ('ssl://'.$tHost.':'.$tPort, $error, $errstr, 30, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $tContext);

    // Check if we were able to open a socket.

    if (!$tSocket)
        $status=false;

    // Build the Binary Notification.
    $tMsg = chr (0) . chr (0) . chr (32) . pack ('H*', $registration_id) . pack ('n', strlen ($tBody)) . $tBody;

    // Send the Notification to the Server.

    $tResult = fwrite ($tSocket, $tMsg, strlen ($tMsg));

    if ($tResult)
        $status=true;
    else
        $status=false;
    fclose ($tSocket);

            return $status;
}`

答案 1 :(得分:0)

发送一些链接希望,以便帮助您在Android设备上发送推送通知。

如果您有任何疑问,请告诉我,我可以帮助您Send Push Notification on android devide