Skip to content

Instantly share code, notes, and snippets.

@klipitkas
Created April 18, 2018 14:23
Show Gist options
  • Select an option

  • Save klipitkas/b7a1df0e356f140266618fff86b996d8 to your computer and use it in GitHub Desktop.

Select an option

Save klipitkas/b7a1df0e356f140266618fff86b996d8 to your computer and use it in GitHub Desktop.
Convert APNS p12 certificate to PEM for use in PHP script

openssl pkcs12 -in aps_cert.p12 -out aps_cert.pem

@klipitkas
Copy link
Copy Markdown
Author

 if ( strcasecmp( $device_type, 'android' ) === 0 ) {
    // Send android push notification

    $notification = array(
      'to' => $device_token,
      'notification' => array(
        'title' => $title,
        'body' => $message,
        'sound' => 'default',
        'badge' => '1'
      ),
      'priority' => 'high'
    );

    if ( ! empty( $data ) ) {
      $notification['data'] = $data;
    }

    $c = curl_init();
    curl_setopt( $c, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
    curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt( $c, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $c, CURLOPT_POSTFIELDS, json_encode( $notification ) );
    curl_setopt( $c, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization: key=' . getenv('FCM_API_KEY')
      )
    );
    $response = curl_exec( $c );
    curl_close( $c );

    if ( $response == false ) {
      return false;
    }

    $response = json_decode( $response );

    return (bool) $response->success;
  } else if ( strcasecmp( $device_type, 'ios' ) === 0 ) {
    // Send iOS push notification
    $environment = getenv( 'PUSH_NOTIFICATIONS_ENV' );
    $certificate = getenv( 'APNS_CERTIFICATE_PATH' );
    $passphrase = getenv( 'APNS_PASSWORD' );

    $ctx = stream_context_create();

    stream_context_set_option( $ctx, 'ssl', 'local_cert', $certificate );
    stream_context_set_option( $ctx, 'ssl', 'passphrase', $passphrase );

    $apns_server = null;

    if ( $environment === 'development' || empty( $environment ) ) {
      $apns_server = 'ssl://gateway.sandbox.push.apple.com:2195';
    } else {
      $apns_server = 'ssl://gateway.push.apple.com:2195';
    }

    $fp = stream_socket_client(
      $apns_server,
      $err,
      $errstr,
      60,
      STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT,
      $ctx
    );

    if (! $fp ) {
      return false;
    }

    $body['aps'] = array(
      'alert' => array(
        'title' => $title,
        'body' => $message,
      ),
      'badge' => 1,
      'sound' => 'default',
    );

    $payload = json_encode( $body );

    $msg = chr(0)
      . pack( 'n', 32 )
      . pack( 'H*', $device_token )
      . pack( 'n', strlen( $payload ) )
      . $payload;

    $result = fwrite( $fp, $msg, strlen( $msg ) );

    if ( ! $result ) {
      return false;
    }

    fclose($fp);

    return true;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment