Send push notification using javaScript and PHP

Поделиться
HTML-код
  • Опубликовано: 17 окт 2024
  • web push library : github.com/web...
    project source code : github.com/ben...

Комментарии • 88

  • @twoxim
    @twoxim 4 месяца назад +2

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

  • @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

  • @pingxtratech
    @pingxtratech 5 месяцев назад

    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  5 месяцев назад

      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)

  • @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.

    • @genesaretjohnes5732
      @genesaretjohnes5732 7 дней назад

      on mine it works. i think some web apis that need secure context are able to be tested if we use localhost.

  • @wildanshalahuddin3824
    @wildanshalahuddin3824 9 месяцев назад +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  9 месяцев назад +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

  • @RuebenTijani
    @RuebenTijani 23 дня назад

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

  • @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.

  • @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!

  • @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.

  • @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

  • @GiaMisseri
    @GiaMisseri 2 месяца назад

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

  • @andrejhohnjec4106
    @andrejhohnjec4106 6 месяцев назад

    Like watching Bob Ross for coding. Excellent!

    • @benixal
      @benixal  6 месяцев назад

      Glad you enjoyed it! Thanks for the kind words!

  • @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.

  • @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. :)

  • @jimpannell2035
    @jimpannell2035 10 месяцев назад

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

    • @benixal
      @benixal  10 месяцев назад

      Glad it was helpful! :)

  • @philippeachache4600
    @philippeachache4600 8 месяцев назад

    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  8 месяцев назад

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

    • @benixal
      @benixal  8 месяцев назад +1

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

    • @philippeachache4600
      @philippeachache4600 8 месяцев назад

      @@benixal You 're great, man!

  • @LeguizChristianA.IT-31WEBAPPDE
    @LeguizChristianA.IT-31WEBAPPDE 11 месяцев назад

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

  • @marjaybumalod2583
    @marjaybumalod2583 6 месяцев назад

    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  6 месяцев назад

      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 6 месяцев назад

      @@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

  • @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())

  • @HoneyBee-kj3db
    @HoneyBee-kj3db 9 месяцев назад

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

  • @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?

  • @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?

  • @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.

  • @vassilismatragos3043
    @vassilismatragos3043 4 месяца назад

    Thanks for this Ben!

  • @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

  • @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?

  • @lwiimbokasweshi
    @lwiimbokasweshi 2 месяца назад

    Excellent video

  • @AmarSingh-e6u5r
    @AmarSingh-e6u5r 9 месяцев назад +1

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

  • @sampahemmanuel2643
    @sampahemmanuel2643 Год назад

    Exactly what I needed. Thanks

  • @iloveyou-kd2lf
    @iloveyou-kd2lf Год назад

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

    • @benixal
      @benixal  Год назад

      Check the console logs.

  • @benitocanfora7667
    @benitocanfora7667 10 месяцев назад

    Great video, thank you :)

    • @benixal
      @benixal  10 месяцев назад

      You are welcome! Glad you liked it!

  • @zenoxgaming3837
    @zenoxgaming3837 9 месяцев назад +1

    Thanks😁❤❤

    • @benixal
      @benixal  9 месяцев назад

      You're welcome! 😊

  • @bamcatiloc5735
    @bamcatiloc5735 7 месяцев назад

    It will work in localhost?

  • @HeryPurnama
    @HeryPurnama 7 месяцев назад +1

    Thank u so much

    • @benixal
      @benixal  7 месяцев назад

      you're welcome.

  • @basically_basic
    @basically_basic 2 месяца назад

    I don't have autoload.php file

  • @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