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?
2 Answers
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
PHP
as 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
PHP
script on server that can trigger notification. I assume you already know how to call aPHP
script, It's same as calling the normalAPI
to do operations inDatabase
.
Having known the process flow you need to follow below steps.
Follow this tutorial to start a
Firebase
project and acquireFCM Notification
functionality. Get firebase api key from the console.In your User application subscribe to a topic using the code below. Put this code in the
onCreate
method of theActivity
after 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 unsubscribe
Put this code in a
PHP
file, 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
PHP
script 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.

- 1,872
- 1
- 13
- 28
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

- 340
- 1
- 7
-
But can my other application be able to get those notifications? – Abhishek VD Apr 22 '21 at 06:04