arrow_upward

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to develop Push Notification in PHP?
#1
Thumbs Up 
Hi,
Currently I am trying to implement a webpush notification in my website, I have tried some tutorials but without a full success. I tried webpush php but I could not use it correctly. Sometimes I can't unsubscribe the user.
Have you tried it anywhere and do you have a complete sample code of it?


Thank you  Sweet



#2
i think this is worth a try:

https://www.phpzag.com/push-notification...php-mysql/
somthing i also found as example (for android):

<?php

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );


$registrationIds = array( $_GET['id'] );

// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title',
'subtitle' => 'This is a subtitle. subtitle',
'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);

$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);

$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;
#3
you can use firebase admin on php and make a push notification with it . send the push through cloud messaging but you need to configure your app to support firebase cloud messaging
you can read the docs here https://firebase-php.readthedocs.io/en/stable/

i personally dont use php when sending push notification i just use it manually with the firebase panel
Terminal
humanpuff69@FPAX:~$ Thanks To Shadow Hosting And Post4VPS for VPS 5
#4
@Littlemaster I'd recommend you to use the one @perryoo11 linked above.

I've used that one from PHPZag and that works perfectly. It should be compatible with the latest 7.x PHP versions as well.

For future reference, PHPZag has amazing PHP tutorials of all kinds. The best part about the team is that all tutorials are quite well-maintained. If you're stuck with anything in PHP you can most probably find a solution there. Smile

Regards,
Sayan Bhattacharyya,

Heartiest thanks to Post4VPS and Virmach for my wonderful VPS 9!
#5
(02-24-2020, 05:35 PM)sohamb03 Wrote: @Littlemaster I'd recommend you to use the one @perryoo11 linked above.

I've used that one from PHPZag and that works perfectly. It should be compatible with the latest 7.x PHP versions as well.


Regards,
I checked the script, I think that is only a browser based push system, what I am trying is to make the push to android top drag screen, not just the browser. I can't see codes for it in those pages.I need to show the push on android homescreen in the style like new email or whatsapp message even the phone is locked.


Thank you  Sweet



#6
Well I'm not quite sure of what you mean. The tutorial Perry linked above sends push notifications on both desktop and mobile browsers.

If you mean, sending push notifications in an Android app, here's a relevant one - https://android.jlelse.eu/android-push-n...58daff2f50

If you need anything else, feel free to lemme know.

Regards,
Sayan Bhattacharyya,

Heartiest thanks to Post4VPS and Virmach for my wonderful VPS 9!
#7
If you are using Firebase push notification to send a notification in android then you can use this code.


PHP Code: (Select All)
<?php

function sendMessage($data$target$serverKey){
   //FCM api URL
   $rsp = [];
   $url 'https://fcm.googleapis.com/fcm/send';
   //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
   $server_key $serverKey;
   $fields = array();
   $fields['data'] = $data;
   if(is_array($target)){
           $fields['registration_ids'] = $target;
       }else{
           $fields['to'] = $target;
   }
   //header with content_type api key
   $headers = array(
       'Content-Type:application/json',
       'Authorization:key='.$server_key
   
);
   
   $ch 
curl_init();
   curl_setopt($chCURLOPT_URL$url);
   curl_setopt($chCURLOPT_POSTtrue);
   curl_setopt($chCURLOPT_HTTPHEADER$headers);
   curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
   curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
   curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
   curl_setopt($chCURLOPT_POSTFIELDSjson_encode($fields));
   $result curl_exec($ch);
   if ($result === FALSE) {
       //die('FCM Send Error: ' . curl_error($ch));
   }
   curl_close($ch);
   
   
//print_r($result);
   return $result;


You can find complete tutorial to send Firebase push notification android


person_pin_circle Users browsing this thread: 1 Guest(s)
Sponsors: VirMach - Host4Fun - CubeData - Evolution-Host - HostDare - Hyper Expert - Shadow Hosting - Bladenode - Hostlease - RackNerd - ReadyDedis - Limitless Hosting