Apple push notifications for iOS and Google cloud messaging for Android using php?

I am looking for php code that can send push notifications to ios devices and android devices.

I have individually used easyapns for ios push notifications and gcm for android push notifications.

I want to combine both codes and make one code to send push notification for both devices.

I only work with the php part. is there any solution to make it easier to implement?

+3


source to share


2 answers


after my research, I found a solution that works great for 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;
}`

      

+3


source


By sending you a trusted link to help you send push notification to your Android device.



If you have any questions please let me know, I can help you Send Push Notification on Android Browser

0


source







All Articles