I've created two different android applications, one for the admin panel which has only one user itself, and the other one which will be installed on other android devices. As the admin adds notifications in his application, all users will get the notifications. what should I do? How to implement this?
How to send notification from one android application to other android application?
1.1k Views Asked by Abhishek VD AtThere are 2 best solutions below
On
You sound a beginner so I will explain in simple words first.
- You need a server side script which can trigger the notification sending code, If you are running
PHPas your server side language then you can use my code below. - Then you need your User application to subscribed to a topic. A topic is just a custom string. You will use this topic string to send notification to specific user or user groups.
- From your Admin application you need to call that
PHPscript on server that can trigger notification. I assume you already know how to call aPHPscript, It's same as calling the normalAPIto do operations inDatabase.
Having known the process flow you need to follow below steps.
Follow this tutorial to start a
Firebaseproject and acquireFCM Notificationfunctionality. Get firebase api key from the console.In your User application subscribe to a topic using the code below. Put this code in the
onCreatemethod of theActivityafter user logs into app. Unsubscribe the topic if you want user to stop receiving notifications.FirebaseMessaging.getInstance().subscribeToTopic("your topic name here"); //to subscribe FirebaseMessaging.getInstance().unsubscribeFromTopic("your topic name here"); //to unsubscribePut this code in a
PHPfile, you need to call this to trigger notification.define('FIREBASE_API_KEY', 'your firebase api key'); $title = "this will display as notification title"; $body = "subject of notification"; $topic = "your topic name"; $message["title"] = $title; $message["body"] = $body; $fields = array( 'to' => '/topics/'.$topic, 'notification' => $message, ); // Set POST variables $url = 'https://fcm.googleapis.com/fcm/send'; $headers = array( 'Authorization: key=' . FIREBASE_API_KEY, 'Content-Type: application/json' ); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Disabling SSL Certificate support temporarly curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); // Execute post $result=''; if($title!=null){ $result = curl_exec($ch); if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); } } // Close connection curl_close($ch);Now call this
PHPscript from your Admin application. You can pass parameters according to your needs. You will be able to get a notification in your User app.
You can customize the notification in your Android code, like playing a specific sound. For that you need to follow the official document guide I linked above.
Use firebase FCM for sending the push notifications to android devices. Here you will find lot of options. https://firebase.google.com/docs/cloud-messaging