How to Send Notification to Android from PHP

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;

How to send push notifications to android mobile from php in react-native?

There may be an issue in the structure of the JSON payload you are sending. According to the documentation here: https://firebase.google.com/docs/cloud-messaging/concept-options, it should be structured as follows:

{
"message":{
"token":"fL8aUT2un*******************************B2bzLa",
"notification":{
"title":"ControlPlus App",
"body":"New Workorder has been added !! "
}
}
}

Cannot send notification from php script to an android app

Actually, there was problem with my $notifyArray variable as I need to pass predefined parameters as defined below in the link:

https://firebase.google.com/docs/cloud-messaging/http-server-ref

$notify=array('to'=>$token,'notification'=>
array('title'=>"Title",body'=>"Subtitle"));

How to send push notifications to multiple devices using php script using FCM?

Try to send device ID of multiple devices as an array. In your case,

$registration_ids must be an array of device IDs.

E.g

$registration_ids = array('Device ID 1', 'Device ID 2');

send push notification to android app using php without external cloud like firebase

You couldn't push notification without using cloud service, but you can request for some data from a server and get a response without using cloud service. You can use network libray like volley and retrofit. Here I shows an example of volley

Add the following dependency in your app gradle

dependencies {
compile 'com.android.volley:volley:1.0.0'
}

Add internet permission in your manifest

<uses-permission android:name="android.permission.INTERNET" />

And add the following in your activity

//specify url
String url = "https:yourIpAddress/file.php";

// make a request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
//Success
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//Error
}
});

//Make a request queue
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());

//Add request to requestQueue
mRequestQueue.add(jsonObjReq);

For more details refer enter link description here



Related Topics



Leave a reply



Submit