<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asadut Zaman</title>
    <description>The latest articles on DEV Community by Asadut Zaman (@asadut_zaman).</description>
    <link>https://dev.to/asadut_zaman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1506490%2Ff2838bab-fea1-4ab2-a34f-81b6ce2d93ed.jpg</url>
      <title>DEV Community: Asadut Zaman</title>
      <link>https://dev.to/asadut_zaman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asadut_zaman"/>
    <language>en</language>
    <item>
      <title>Laravel for IOS push notification</title>
      <dc:creator>Asadut Zaman</dc:creator>
      <pubDate>Fri, 28 Jun 2024 15:28:40 +0000</pubDate>
      <link>https://dev.to/asadut_zaman/apns-apple-push-notification-service-using-laravel-3odc</link>
      <guid>https://dev.to/asadut_zaman/apns-apple-push-notification-service-using-laravel-3odc</guid>
      <description>&lt;p&gt;In this post, I will discuss the only Laravel end functionality, assuming you have 'certificate.p12', 'password', and 'app_bundel_id' from Apple &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;br&gt;
If you have 'Certificates.p12' you need to convert this to 'Certificates.pem' file. I have stored my 'Certificates.p12' file in the 'storage' directory.&lt;/p&gt;

&lt;p&gt;In .env&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;APN_ENVIRONMENT=sandbox
APN_CERTIFICATE_PATH=storage/Certificates.p12
APN_CERTIFICATE_PASSWORD=123456
APN_APP_BUNDLE_ID=com.btrac.ems
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run the below comment to your project terminal&lt;br&gt;
&lt;code&gt;openssl pkcs12 -in storage/Certificates.p12 -out storage/Certificates.pem -nodes -clcerts&lt;/code&gt;&lt;br&gt;
that command convert my 'Certificates.p12' file into 'Certificates.pem' file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwogf0o32v7brprnyasf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwogf0o32v7brprnyasf.png" alt="Image description" width="365" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;br&gt;
Create app/helper.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

if (! function_exists('createApnPayload')) {
    function createApnPayload($title, $body) {
        return json_encode([
            'aps' =&amp;gt; [
                'alert' =&amp;gt; [
                    'title' =&amp;gt; $title,
                    'body' =&amp;gt; $body,
                ],
                'sound' =&amp;gt; 'default',
            ],
        ]);
    }
}

if (! function_exists('sendApnNotification')) {
    function sendApnNotification($deviceToken, $payload) {
        $certificatePath = storage_path('Certificates.pem');
        $passphrase = env('APN_CERTIFICATE_PASSWORD', ''); // Use the passphrase if set, otherwise empty

        $url = "https://api.sandbox.push.apple.com/3/device/$deviceToken"; // Use 'api.push.apple.com' for production

        $headers = [
            'apns-topic: ' . env('APN_APP_BUNDLE_ID'),
            'Content-Type: application/json',
        ];

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_PORT, 443);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSLCERT, $certificatePath);
        curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $passphrase);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception('cURL error: ' . curl_error($ch));
        }

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);

        if ($httpCode == 200) {
            return 'Notification sent successfully';
        } else {
            return 'Notification failed with HTTP code ' . $httpCode . ' and response ' . $response;
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;br&gt;
Create a Controller for example 'NotificationController.php'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function sendNotification()
    {
        $deviceToken = 'Your_device_token';
        $title = 'Hello';
        $body = 'Hello from the other side';

        $payload = createApnPayload($title, $body);

        try {
            $result = sendApnNotification($deviceToken, $payload);
            return response()-&amp;gt;json(['message' =&amp;gt; $result]);
        } catch (\Exception $e) {
            return response()-&amp;gt;json(['error' =&amp;gt; 'Notification failed: ' . $e-&amp;gt;getMessage()], 500);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;br&gt;
Make a new route for your notification function in web.php&lt;br&gt;
&lt;code&gt;Route::get('/sendNotification3', [NotificationController::class, 'sendNotification']);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you can test the notification functionality form Postman or browser or call the function from any other function depending on your business logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Save device token&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function userDeviceToken(Request $request, $profileId)
    {
        $token = $request-&amp;gt;device_token;

        $user = User::find($profileId);

        if ($user) {
            $user-&amp;gt;device_token = $token;
            $user-&amp;gt;save();
            $response = [
                'code' =&amp;gt; 200,
                'message' =&amp;gt; 'Device token updated successfully',
                'device_token' =&amp;gt; $token
            ];
            return response()-&amp;gt;json([
                'results' =&amp;gt; $response
            ], 200);
        } else {
            return response()-&amp;gt;json([
                'results' =&amp;gt; [
                    'code' =&amp;gt; 404,
                    'message' =&amp;gt; 'User not found'
                ]
            ], 404);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that you need to be more careful about error handling since in this post I have used CURL rather than any packages&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Create laravel project on Project IDX</title>
      <dc:creator>Asadut Zaman</dc:creator>
      <pubDate>Sat, 15 Jun 2024 03:10:19 +0000</pubDate>
      <link>https://dev.to/asadut_zaman/create-laravel-project-on-project-idx-3aho</link>
      <guid>https://dev.to/asadut_zaman/create-laravel-project-on-project-idx-3aho</guid>
      <description>&lt;p&gt;Google has launched a beta version of project IDX, an IDE that works on browsers. &lt;/p&gt;

&lt;p&gt;To create a new laravel project click go the &lt;a href="https://idx.google.com/" rel="noopener noreferrer"&gt;idx.google.com&lt;/a&gt; and "Select all the templates"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2xud1il3zke8u92w1a4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2xud1il3zke8u92w1a4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Backend &amp;gt; laravel &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frylzjxu0pcm8268cobz8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frylzjxu0pcm8268cobz8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give a name to your project &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl33cwyoqbfep3ouxx0s1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl33cwyoqbfep3ouxx0s1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yf8dsvop3rfbvqb7awf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yf8dsvop3rfbvqb7awf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here you go, your laravel is ready to play&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrvjsncg82z938tn8e29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrvjsncg82z938tn8e29.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can share the link that can be accessible over the internet.&lt;br&gt;
The IDX has all the features just like the VS Code &lt;/p&gt;

&lt;p&gt;However, IDX is now supporting only one project. Here you can delete share or rename your project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0u97wrygp50480ifvvl8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0u97wrygp50480ifvvl8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
