Push notifications allow developers to reach users, even when users aren’t actively using an app! In this article, you’ll learn how to configure your app to receive push notifications and to display them to your users or perform other tasks.
Push notification to iOS devices, In other words, APNs (Apple Push Notification service )
Service for iOS application developers to propagate information to push notifications to iOS devices and indirectly watchOS, tvOS, and macOS devices.
Follow the steps here to obtain and configure your APNs credentials
Step 1. Make sure, PHP extension php_openssl is enabled and port 2195 is free on the server.
Step 2. Generate certificate ck.pem from iOS Dev Center(https://developer.apple.com/ios/).
Step 3. Write below code in PHP file using your “ck.pem”, iOS “$deviceToken”(which are generating from an iOS device), and passphrase(use blank value if the passphrase is blank).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
//$device_token=Token generate by ios device $device_token = "csniusvusbviskdjnsdkvsdbvjsdbjshsvdsds"; $deviceToken = "" . $device_token . ""; //$passphrase=When you generate ck.pem used inside $passphrase = 'passpharase'; //Ck.pem half path of server $ck_pem_path = "ck.pem"; //When your application live then change development to production $development = "development"; if ($development == "development") { $socket_url = "ssl://gateway.sandbox.push.apple.com:2195"; } else { $socket_url = "ssl://gateway.push.apple.com:2195"; } $message_body = array( 'type' => 1, 'alert' => 'Test notification', 'badge' => 0, 'sound' => 'newMessage.wav' ); $ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', $ck_pem_path); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); // Open a connection to the APNS server $fp = stream_socket_client( $socket_url, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx ); if (!$fp) $error = "Failed to connect: $err $errstr" . PHP_EOL; $body['aps'] = $message_body; $payload = json_encode($body); // Build the binary notification $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // Send it to the server $result = fwrite($fp, $msg, strlen($msg)); fclose($fp); if (!$result) { $return = "Error, notification not sent" . PHP_EOL; } else { $return = "Success, notification sent"; } echo $return; |
New Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
$message_array = array( 'type' => '1', 'id' => '2', 'alert' => 'Test notification', 'badge' => 0, 'sound' => 'newMessage.wav' ); $deviceToken="e7f2a65bfb98e86e624643ff7cedd8c8996152f02c85ffafd0d0e92644d14182"; $keyfile = '/var/www/html/laravel/public/AuthKey_CYJN9MWVWL.p8'; # <- Your AuthKey file $keyid = 'IDID9MWVWL'; # <- Your Key ID $teamid = 'TETEBC4TXU'; # <- Your Team ID (see Developer Portal) $bundleid = 'com.package name'; # <- Your Bundle ID $url = 'https://api.sandbox.push.apple.com'; # <- development url, or use http://api.push.apple.com for production environment $token = $deviceToken; # <- Device Token $body = array(); $body['aps'] = $message_object; // Encode the payload as JSON $message = json_encode($body); //$message = '{"aps":{"alert":"Hi there!","sound":"default"}}'; $key = openssl_pkey_get_private('file://' . $keyfile); $header = ['alg' => 'ES256', 'kid' => $keyid]; $claims = ['iss' => $teamid, 'iat' => time()]; $header_encoded = base64ForPush($header); $claims_encoded = base64ForPush($claims); $signature = ''; openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256'); $jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature); // only needed for PHP prior to 5.5.24 if (!defined('CURL_HTTP_VERSION_2_0')) { define('CURL_HTTP_VERSION_2_0', 3); } $http2ch = curl_init(); curl_setopt_array($http2ch, array( CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, CURLOPT_URL => "$url/3/device/$token", CURLOPT_PORT => 443, CURLOPT_HTTPHEADER => array( "apns-topic: {$bundleid}", "authorization: bearer $jwt", ), CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $message, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_TIMEOUT => 30, CURLOPT_HEADER => 1, )); $result = curl_exec($http2ch); if ($result === FALSE) { throw new Exception("Curl failed: " . curl_error($http2ch)); } $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE); return $status; |
Optional settings
Badge: The default value is zero (0), and this would not display a badge.
Sound: Supports default or the name of a sound resource bundled in the app.
Additional payload: Specifies the custom payload values for your notifications to manage your application flow.
It’s works fine
Thanks