Android Push Notification via PHP in Ionic/Cordova
3:26 PM-
If you are using phonegap then install phonegap push notification plugin via
ORcordova plugin add phonegap-plugin-push
If you are working on native coding, You've to install Android Push Notificaion SDK - Create a project on Google Developers Console and enable "Cloud Messaging for Android" API for this project
-
This code execute after device ready :
var push = PushNotification.init({ "android" : { "senderID" : "9XX5XXX346XX"// This is your project number which you've created on Google Developers Console }, "ios" : {}, "windows" : {} }); push.on('registration', function(data) { //data.registrationId //send this token on server }); push.on('notification', function(data) { if (data.sound) {// play specific sound on notification received(When app running) var snd = new Media('file:///android_asset/www/sound/' + data.sound); snd.play(); } if (!data.additionalData.foreground) { // app is NOT RUNNING and new notification is RECEIVED } else { // app is RUNNING and new notification is RECEIVED } }); push.on('error', function(e) { //alert(JSON.stringify(data)) // e.message });
-
PHP Code for send push notification
$api_key = "AXXaSyXXyKXXNZXXf8O27hXX2DcrXXAEeXXuXX0";// your Google Developers Console Project API key $data = array( "message" => $message, 'title' => $title, ); // URL to POST to $gcm_url = 'https://android.googleapis.com/gcm/send'; // data to be posted $fields = array('registration_ids' => $registration_ids, 'data' => $data, ); // headers for the request $headers = array('Authorization: key=' . $api_key, 'Content-Type: application/json'); $curl_handle = curl_init(); //echo 'Notification PhP file'; // set CURL options curl_setopt($curl_handle, CURLOPT_URL, $gcm_url); curl_setopt($curl_handle, CURLOPT_POST, true); curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($fields)); // send $response = curl_exec($curl_handle); curl_close($curl_handle);
15 comments
Hi,
ReplyDeleteDo we need to publish our app on playstore to use this url?
https://android.googleapis.com/gcm/send
Yes, you've to pass $gcm_url = 'https://android.googleapis.com/gcm/send';
ReplyDeletewhere is the $registration_ids definition?
ReplyDeletepush.on('registration', function(data) {
ReplyDelete//data.registrationId
//send this token on server
});
Form this code you'll find the $registration_ids(this is part of cordova plugin). You've to save this $registration_ids(data.registrationId) with user id in a table and at the time of send notification you can find the $registration_ids(data.registrationId) in your table
nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.
ReplyDeletedot net training in chennai
Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android training in chennai with placement | Android Training in chennai
ReplyDeleteI wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeleteTexting API
Text message marketing
Digital Mobile Marketing
Sms API
Sms marketing
I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
ReplyDeletePSD to Wordpress
wordpress website development
This comment has been removed by the author.
ReplyDeleteI really enjoyed your blog Thanks for sharing and it was very usefully to me
ReplyDeleteIonic Online Training
Ionic Training in Hyderabad
Ionic Online Ameerpet
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.keep it up!!
ReplyDeleteandroid training in chennai
android online training in chennai
android training in bangalore
android training in hyderabad
android Training in coimbatore
android training
android online training
Hello, also a simple way is with INDIGITALL, it has been developed with the latest technology to guarantee maximum effectiveness and simplify the process of creating and sending Notifications with an animated image, Segmented, Geolocated and many more functions, it is great. Here you can see the simple installation for your platform
ReplyDeletehttps://docs.indigitall.com/
Good to see such a nice blog post.
ReplyDeleteVisit us: RPA Ui Path Online Training
Visit us: Ui Path Course Online
Yeni perde modelleri
ReplyDeleteNUMARA ONAY
VODAFONE MOBİL ÖDEME BOZDURMA
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
site kurmak
aşk kitapları
smm panel
ReplyDeletesmm panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
https://www.beyazesyateknikservisi.com.tr/
servis
tiktok jeton hilesi