Send push notification using javaScript and PHP

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ต.ค. 2024

ความคิดเห็น • 89

  • @twoxim
    @twoxim 3 หลายเดือนก่อน +2

    One of the best instructional videos, and I've watched 1000s. Easy to follow and understand! Keep up the great work!!!

  • @sebaszwarc
    @sebaszwarc 6 หลายเดือนก่อน +2

    ok, the reason navigator is undefined is because notification works only with SSL environment - author didn't mention that at all and because he is using localhost it's even more confusing because you cannot setup https on localhost - certbot will complain as this is not a domain.

  • @GiaMisseri
    @GiaMisseri หลายเดือนก่อน

    I can get this working on desktop browsers, but the subscription data is not being generated on iOS for me to save... help please?

  • @AmarSingh-e6u5r
    @AmarSingh-e6u5r 9 หลายเดือนก่อน +1

    in my case
    .then((subscription)=> {
    console.log(JSON.stringify(subscription));
    });
    i am not able to see subscription

    • @caseysky102
      @caseysky102 2 หลายเดือนก่อน

      same here

  • @sebaszwarc
    @sebaszwarc 6 หลายเดือนก่อน

    I was expecting to find proper tutorials but yours has errors - Cannot read properties of undefined (reading 'register'). navigator is undefined anywhere. how can you post code without checking

    • @benixal
      @benixal  6 หลายเดือนก่อน

      Thanks for letting me know! Could you please tell me which browser you were using when you encountered the error?

  • @wildanshalahuddin3824
    @wildanshalahuddin3824 8 หลายเดือนก่อน +1

    i'v successed implement your code in plain php, but when i use codeigniter 3, why i cant get the endpoint in the console.log ? its just blank, not error, just blank. Like something block this code "then((subscription)" in the codeigniter view, Can you help me ?

    • @benixal
      @benixal  8 หลายเดือนก่อน +1

      Make sure that the Service Worker is registered: (06:11)
      and I checked this with CodeIgniter 3, and I was able to get the subscription data:
      github.com/benixal/CodeIgniter-3-pushManager-subscription-example

  • @basically_basic
    @basically_basic 2 หลายเดือนก่อน

    I don't have autoload.php file

  • @marjaybumalod2583
    @marjaybumalod2583 5 หลายเดือนก่อน

    how to fix this " Uncaught RuntimeException: Unable to create the key in C:\xampp\htdocs\push_notification\vendor\web-token\jwt-library\Core\Util\ECKey.php:98" ?

    • @benixal
      @benixal  5 หลายเดือนก่อน

      The error 'Unable to create the key in ....\ECKey.php' is likely because OpenSSL isn't turned on.
      To fix this in XAMPP:
      Open the php.ini file.
      Look for ;extension=openssl.
      Take out the semicolon before it.
      Save and restart XAMPP.
      If you don't have OpenSSL, you can use a website to make VAPID keys. Just search "generate VAPID keys." But remember, this is mainly for practice, not real projects

    • @marjaybumalod2583
      @marjaybumalod2583 5 หลายเดือนก่อน

      @@benixal OpenSSL is enabledPHP Fatal error: Uncaught RuntimeException: Unable to create the key in C:\xampp\htdocs\push_notification\vendor\web-token\jwt-library\Core\Util\ECKey.php:98
      Here is the another error

  • @RuebenTijani
    @RuebenTijani 6 วันที่ผ่านมา

    😂 push notif. The laziness to type out full words gets me too 😂😂

    • @benixal
      @benixal  6 วันที่ผ่านมา

      😂✌️

  • @zenoxgaming3837
    @zenoxgaming3837 8 หลายเดือนก่อน +1

    Thanks😁❤❤

    • @benixal
      @benixal  8 หลายเดือนก่อน

      You're welcome! 😊

  • @HeryPurnama
    @HeryPurnama 6 หลายเดือนก่อน +1

    Thank u so much

    • @benixal
      @benixal  6 หลายเดือนก่อน

      you're welcome.

  • @Andy-zr8pw
    @Andy-zr8pw ปีที่แล้ว +1

    if my project use php v 7.4, what it is work ?

    • @benixal
      @benixal  ปีที่แล้ว

      Yes , but to install `minishlink/web-push` (09:38) ... based on the README from github.com/web-push-libs/web-push-php,
      ```
      PHP 5.6 or HHVM: v1.x
      PHP 7.0: v2.x
      PHP 7.1: v3.x-v5.x
      PHP 7.2: v6.x
      PHP 7.3 7.4: v7.x
      ```
      you should install version 7.x for PHP 7.4 compatibility.
      Here's the command to install it:
      composer require minishlink/web-push:^7.0

    • @Andy-zr8pw
      @Andy-zr8pw ปีที่แล้ว

      thanks you @@benixal

  • @HoneyBee-kj3db
    @HoneyBee-kj3db 9 หลายเดือนก่อน

    Where should I put each file if I use CodeIgniter 4?

  • @lwiimbokasweshi
    @lwiimbokasweshi 2 หลายเดือนก่อน

    Excellent video

  • @pingxtratech
    @pingxtratech 4 หลายเดือนก่อน

    This is so good. Thank you.
    I'd like to ask, the notification will only work while the browser is opened yes? Is there a workaround for sending notifications while the browser is closed?

    • @benixal
      @benixal  4 หลายเดือนก่อน

      You're welcome, thanks for your comment,
      Notifications will be received if the browser is fouced or in the background, (even if your webpage is not opened). On mobile devices: browsers are usually running in the background, so notifications will usually be received. However, on desktops, if the user completely closes the browser, notifications won't be received and they'll receive them immediately upon reopening the browser (there's no need to open your website, just run the browser)

  • @sampahemmanuel2643
    @sampahemmanuel2643 ปีที่แล้ว

    Hi Beni, assuming I host my PHP app on Digital Ocean or a Shared Server, can the push notifications still work. Thank you

    • @benixal
      @benixal  ปีที่แล้ว

      Hi Sampah, according to the README (github.com/web-push-libs/web-push-php#readme), if your hosting provider supports mbstring, curl, and openssl, and you're using PHP 8, 7, or even 5.6, then you can definitely use the web-push-php library for push notifications.

  • @abdulhameednabhan5595
    @abdulhameednabhan5595 ปีที่แล้ว

    What should I put after the word send in link?
    server kay from fairbase messaging token?

    • @benixal
      @benixal  ปีที่แล้ว

      Sorry, but I did not understand your question
      Could you please clarify your question or let me know the specific time stamp in the video you're referring to?

  • @andrejhohnjec4106
    @andrejhohnjec4106 5 หลายเดือนก่อน

    Like watching Bob Ross for coding. Excellent!

    • @benixal
      @benixal  5 หลายเดือนก่อน

      Glad you enjoyed it! Thanks for the kind words!

  • @oriamergi5445
    @oriamergi5445 ปีที่แล้ว

    Thanks for the tutorial!
    I'm stuck in getting the end point after creating the "enableNotif" button. The promise isn't resolved.
    Up until that point we only used the backend to create public & private keys which we pasted (only the public) in "applicationServerKey" right?

    • @benixal
      @benixal  ปีที่แล้ว

      You're welcome! Yes, use the public key for the applicationServerKey .
      and please provide more details about the error you are getting in the console.

  • @LeguizChristianA.IT-31WEBAPPDE
    @LeguizChristianA.IT-31WEBAPPDE 10 หลายเดือนก่อน

    is that ok to use only the first part of the video where you not using a library

  • @fonnets
    @fonnets ปีที่แล้ว +1

    Hi, the tutorial is very clear and complete! I managed to replicate the example and everything works. However I don't understand how to manage subscriptions, because to send a notification I have to insert the json of the subscription into the function. This example works with only one browser but if I have to send notifications to multiple browsers do I have to keep all subscriptions and send each one? Thank you

    • @benixal
      @benixal  ปีที่แล้ว +2

      Hi, I'm glad the tutorial was helpful.
      Yes, to send notifications to multiple browsers, you should store all subscription data in your database and then send notifications to each one you want.
      But if the number of subscriptions is high
      For example, you want to send a group notification to 10,000 people
      It is better to do this on a scheduled basis, for example, once every few seconds .. or use several servers and networks
      But if the number is much more than this, it is better to use other services such as FCM that allows you to send push notification to very big group of subscriptions.

    • @fonnets
      @fonnets ปีที่แล้ว

      Hi Ben! Now is all clear! Thank you!@@benixal

    • @bst-football
      @bst-football ปีที่แล้ว +1

      @@benixal hi what is FCM?

    • @meirbek241
      @meirbek241 5 หลายเดือนก่อน

      ​@@benixal Hello, Ben. Thank you for the great tutorial.
      Do you know where can I read about limitations in terms of number of people for sending push notifications using current approach?
      For example if I want to send push notifications to around 100 users (single notification to a single device) on a daily basis, will current approach work properly?

    • @geplaksisilla2742
      @geplaksisilla2742 3 หลายเดือนก่อน

      @@benixal Dear Benixal! Very useful tutorial, thank you so much! Can you write a sample how can i send more then one subscription?

  • @bamcatiloc5735
    @bamcatiloc5735 6 หลายเดือนก่อน

    It will work in localhost?

    • @benixal
      @benixal  6 หลายเดือนก่อน

      Yes.

    • @bamcatiloc5735
      @bamcatiloc5735 6 หลายเดือนก่อน

      Thanks bro

  • @vassilismatragos3043
    @vassilismatragos3043 4 หลายเดือนก่อน

    Thanks for this Ben!

  • @falinhares
    @falinhares ปีที่แล้ว

    I am getting an error when going for the application test: Cannot read properties of undefined (reading 'showNotification') on line event.waitUntil(self.registration.showNotification(notification.title, {...

    • @falinhares
      @falinhares ปีที่แล้ว

      Found a type and corrected, but you have to click to update the service. It doesn't update by itself...

    • @benixal
      @benixal  ปีที่แล้ว

      That's due to browser caching.
      add a unique random query parameter when registering the service worker each time you update it :
      navigator.serviceWorker.register('sw.js?v=2')
      or
      navigator.serviceWorker.register('sw.js?v=' + Math.random())

  • @nishants3967
    @nishants3967 ปีที่แล้ว

    Hi Ben, how do I do with SSE server in Node.js, and PHP pages (pre-render) consumes this SSE events. I wrote few snippets but its not working in onmessage. Help appreciated. Thanks.

    • @benixal
      @benixal  ปีที่แล้ว

      Hi Nishant , sorry not familiar with that , thanks for your comment.

    • @nishants3967
      @nishants3967 ปีที่แล้ว

      @@benixal No issue. :)

  • @iloveyou-kd2lf
    @iloveyou-kd2lf ปีที่แล้ว

    7:03 I cliked push button. but it is not showing popup. Why?

    • @benixal
      @benixal  ปีที่แล้ว

      Check the console logs.

  • @philippeachache4600
    @philippeachache4600 7 หลายเดือนก่อน

    problem: I m stuck with the file send.php about [reason:protected] =>curl error 60, is there a issue to solve it easily

    • @benixal
      @benixal  7 หลายเดือนก่อน

      Check this out: github.com/web-push-libs/web-push-php/issues/93#issuecomment-421913383

    • @benixal
      @benixal  7 หลายเดือนก่อน +1

      change this line:
      $webPush = new WebPush($auth);
      to this:
      $webPush = new Minishlink\WebPush\WebPush($auth, [], 30, ['verify' => false]);

    • @philippeachache4600
      @philippeachache4600 7 หลายเดือนก่อน

      @@benixal You 're great, man!

  • @sampahemmanuel2643
    @sampahemmanuel2643 ปีที่แล้ว

    Hi Beni, please how do I position the notification at the bottom right of the screen when the browser is closed or not

    • @benixal
      @benixal  ปีที่แล้ว

      No, you can't control the position of browser push notifications. They appear according to the browser's default settings.

  • @FabianD1991
    @FabianD1991 ปีที่แล้ว

    Will these notifications be displayed on the mobile? At least with the browser in the background. Thanks!

    • @benixal
      @benixal  ปีที่แล้ว +1

      Absolutely! Notifications will appear on mobile devices, even if the browser is in the background. Thanks for your question!

    • @fonnets
      @fonnets ปีที่แล้ว

      I tested notifications with iPhone (iOS), it works great! You just need to add web apps to the home screen from Safari first.

  • @jimpannell2035
    @jimpannell2035 9 หลายเดือนก่อน

    Thanks for this Ben - I've found it extremely helpful 🙏

    • @benixal
      @benixal  9 หลายเดือนก่อน

      Glad it was helpful! :)

  • @cyriljimrylgana3243
    @cyriljimrylgana3243 ปีที่แล้ว

    is this also function in mobile devices?

  • @fonnets
    @fonnets ปีที่แล้ว

    Hi Ben, after successfully testing everything locally (Mac, MAMP and domain localhost) I tried to test the push notification using an aws ec2 instance and a domain with ssl certificates, but the push notification doesn't arrive...
    Printing the notification response looks ok, so it's not clear what's stopping the notification from arriving. I have tried with Chrome, Safari and Firefox. My server side stack is ubuntu 22 / apache2 / php 8.1. Do you have any idea? Thanks

    • @benixal
      @benixal  ปีที่แล้ว

      Hi there! Thanks for your message , Sorry, I'm not familiar with AWS EC2 instances. Consider asking on AWS forums or Stack Overflow for help. Best of luck!

    • @fonnets
      @fonnets ปีที่แล้ว

      I thought they were problems for notifications from remote servers, since initially only locally it worked perfectly. I did various tests, even with serverless php services, then I tried and tried again, and now I can say that it also works as a remote server with a domain with ssl certificates. It works really well! Thanks again Ben!

    • @benixal
      @benixal  ปีที่แล้ว +1

      You're welcome!

  • @benitocanfora7667
    @benitocanfora7667 10 หลายเดือนก่อน

    Great video, thank you :)

    • @benixal
      @benixal  10 หลายเดือนก่อน

      You are welcome! Glad you liked it!

  • @sampahemmanuel2643
    @sampahemmanuel2643 ปีที่แล้ว

    Exactly what I needed. Thanks

    • @benixal
      @benixal  ปีที่แล้ว

      You're welcome!

  • @diegovillafane6313
    @diegovillafane6313 ปีที่แล้ว

    Hi Beni, great content, can I ask you, are you runing the page in a live server or what are you doing to run it?

    • @diegovillafane6313
      @diegovillafane6313 ปีที่แล้ว

      I used live server so it's ok, THANKS A LOT!!!

    • @benixal
      @benixal  ปีที่แล้ว +1

      You're welcome!
      Thanks for your comment.
      In the video, I used Apache web server (locally) on Ubuntu.
      However, you can run the page on any web server of your choice

  • @nicholasosinski4097
    @nicholasosinski4097 11 หลายเดือนก่อน

    I tried so many guides for how to do this and yours worked... the first time! Thank you so much for the video and for the source code. Thank you!

    • @benixal
      @benixal  11 หลายเดือนก่อน

      Thanks, Nicholas! Glad it worked for you!

    • @nicholasosinski4097
      @nicholasosinski4097 11 หลายเดือนก่อน

      I don't suppose you have an example for queueNotification & flush? Thanks again!@@benixal

    • @nicholasosinski4097
      @nicholasosinski4097 11 หลายเดือนก่อน

      Never mind ...I figured it out. Thank you so much, again! 😀

    • @sebaszwarc
      @sebaszwarc 6 หลายเดือนก่อน

      Problem is it's not working

  • @abdulhameednabhan5595
    @abdulhameednabhan5595 ปีที่แล้ว

    First of all, I would like to thank you for this wonderful content. Please help me. I am a beginner
    First, I had problems when generating the key, so I used the same keys that you used, I mean the keys that we generate with php, does this affect? This is the error
    Fatal error: Uncaught RuntimeException: Unable to create the key in D:\xampp\htdocs\testno\vendor\web-token\jwt-core\Util\ECKey.php:98 Stack trace: #0 D:\xampp\htdocs\ testno\vendor\web-token\jwt-core\Util\ECKey.php(72): Jose\Component\Core\Util\ECKey::createECKeyUsingOpenSSL('P-256') #1 D:\xampp\htdocs\testno \vendor\web-token\jwt-key-mgmt\JWKFactory.php(69): Jose\Component\Core\Util\ECKey::createECKey('P-256', Array) #2 D:\xampp\htdocs\ testno\vendor\minishlink\web-push\src\VAPID.php(165): Jose\Component\KeyManagement\JWKFactory::createECKey('P-256') #3 D:\xampp\htdocs\testno\VAPID.php (6): Minishlink\WebPush\VAPID::createVapidKeys() #4 {main} thrown in D:\xampp\htdocs\testno\vendor\web-token\jwt-core\Util\ECKey.php on line 98 .. ..... My second question is the value here, where should I get it from? For information, I am a Firebase subscriber
    I mean the value in the link...
    $report = $webPush->sendOneNotification(
    Subscription::create(json_decode('{"endpoint":"fcm.googleapis.com/fcm/send/eOmO_2Z-nHA:APA91bHDoFG8hTpuf11C9t_rVP2IOIOcpDimDLF06DYI9NtRPnQlpRN89KeLP2rl6w8C7WryiEPzkCg3XBwQxm hfL36JNMKGdtNSwd4UegS-7sByuGlYjg6FubHASYrrHVcTfefZaV9W","expirationTime":null,"keys":{"p256dh ":"BNBaksmindsZK9u_mghq-Omb1_9bN-hJVP8KjLWB6mlHPf_R3JLmyd-0LwYBGErAjItB2Pex6bAKYFFR_gDdYpo","auth":"H9M9HgHX4a3xmcChKQNWFA"}}',true))
    , '{"title":"Hi from php" , "body":"php is amazing!" , "url":"./?message=123"}', ['TTL' => 5000]);
    print_r($report);

    • @benixal
      @benixal  ปีที่แล้ว

      Thanks for your comment, Abdul Hameed !
      The error "Unable to create the key in ....\ECKey.php:98..." usually shows up when a needed tool called OpenSSL isn't turned on. To fix this in XAMPP:
      Open the php.ini file.
      Look for ;extension=openssl.
      Take out the semicolon before it.
      Save and restart XAMPP.
      If you don't have OpenSSL, you can use a website to make VAPID keys. Just search "generate VAPID keys." But remember, this is mainly for practice, not real projects
      For your second question:
      The subscription data comes when the user runs the enableNotif() function (14:20).
      The 'fcm' part you noticed is because Chrome uses FCM to send notifications,
      while if the user uses Firefox , it'll use 'services.mozilla.com'.
      You don't have to make the subscription data, like the endpoint or anything like that.
      Just use JSON.stringify (13:20), and then you can send notifications with it (18:08).

    • @abdulhameednabhan5595
      @abdulhameednabhan5595 ปีที่แล้ว

      Please what will this line look like can you write it for me?
      $report = $webPush->sendOneNotification(
      Subscription::create(json_decode('{"endpoint":"fcm.googleapis.com/fcm/send/eOmO_2Z-nHA:APA91bHDoFG8hTpuf11C9t_rVP2IOIOcpDimDLF06DYI9NtRPnQlpRN89KeLP2rl6w8C7WryiEPzkCg3XBwQxmhfL36JNMKGdtNSwd4UegS-7sByuGlYjg6FubHASYrrHVcTfefZaV9W","expirationTime":null,"keys":{"p256dh":"BNBaksmindsZK9u_mghq-Omb1_9bN-hJVP8KjLWB6mlHPf_R3JLmyd-0LwYBGErAjItB2Pex6bAKYFFR_gDdYpo","auth":"H9M9HgHX4a3xmcChKQNWFA"}}',true))
      , '{"title":"Hi from php" , "body":"php is amazing!" , "url":"./?message=123"}', ['TTL' => 5000]);@@benixal