PHP - Push Notifications

Push notifications in PHP. How to use it?

I took a key from this page https://web-push-codelab.appspot.com/. But
i don't understand why do they give a private key ?

The server is supposed to generate a public/private keypair. The public key is used by the browser to create a subscription. The private key remains on the server and is used to encrypt the push message.

And i would like also target the notifications. I would like to send
the notification like "xxx commented to your post" or "xxx liked to
your post" or "xxx send to you a friend request"

At some point you have the browser register a subscription by calling pushManager.subscribe (but this code is missing from your question). This returns a promise that resolves to a subscription. You should associate this subscription to the currently authenticated user. For example, you can use AJAX to send the subscription to your server and store it with the currently authenticated user. Whenever you want to notify this user you can use the subscription that is associated with this user.

You might want to take a look at https://web-push-book.gauntface.com/.

PHP: Sending Push notification to ios

A call to fwrite() will return the number of bytes it successfully sent, or it will return FALSE if there was an error sending. Your $result is changing because it changes with the size of the message you are sending. So knowing that, you can surmise that if $result===FALSE then there was an error and the notification failed. If $result!==FALSE, then the notification was successfully sent. This only verifies that the message was sent over the network. It does not verify that the structure of the message or the validity of the tokens or anything like that.

Now, if you trying to find out if the push notification message itself is valid and that apple accepted and is processing it, then you probably want to CURL and HTTP/2 to do this. Doing a quick google search I came across this site that shows a demo how to do it. I have not tested this site http://coding.tabasoft.it/ios/sending-push-notification-with-http2-and-php/ so cannot tell you if the demo is correct or not. It was just a quick search but might get you on the right track.

How to send Notification to Android from php?

I have decided to post answer for my own question

Now Google introduced FCM

<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token='235zgagasd634sdgds46436';

$notification = [
'title' =>'title',
'body' => 'body of message.',
'icon' =>'myIcon',
'sound' => 'mySound'
];
$extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

$fcmNotification = [
//'registration_ids' => $tokenList, //multple token array
'to' => $token, //single token
'notification' => $notification,
'data' => $extraNotificationData
];

$headers = [
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification));
$result = curl_exec($ch);
curl_close($ch);

echo $result;

Web Push Notification: How to use Web Push PHP Library?

Take a look at https://web-push-book.gauntface.com/ for a general introduction to Web Push. The Chapter How Push Works and Subscribing a User should be particularly interesting to you. In summary:

  • At the client side you need to create a subscription by calling pushManager.subscribe. More info here.
  • You should send this subscription to your server. For example, make an AJAX request to send the subscription (endpoint, keys.p256dh, keys.auth) to the server. More info here.
  • On the server you send a push message using the PHP Web Push library. First, you need to configure this library with your keypair. Then, you need to use the subscription (endpoint, keys.p256dh, keys.auth) to send a push message. More info here.


Related Topics



Leave a reply



Submit