This is an Android app written in Kotlin that receives push notifications from a Python script. The app uses Firebase Cloud Messaging and the Realtime Database to receive and display push notifications.
You will need the following to use this code:
- Android Studio and Python installed on your computer
- Your own
google-services.json
and[project_name]-firebase-adminsdk.json
files, which you can download from the Firebase console
Follow these steps to create a new project on the Firebase console, get the FCM private token, and download the necessary files:
- Go to the Firebase Console.
- Sign in with your Google account if you haven't already.
- Click on "Add project."
- Enter a project name and follow the on-screen instructions to set up your project. You can leave the other settings as their defaults.
- Once the project is created, you'll be redirected to the project dashboard.
- From the left-side menu, click on the gear icon (⚙️) and select "Project settings."
- In the "General" tab, scroll down to find the "Your apps" section.
- Click on the Android icon to add an Android app to your project.
- Fill in the "Android package name" and "App nickname" fields. The package name is
com.example.PMRTest
. - Click "Register app."
- Download the
google-services.json
file by clicking on the "Download google-services.json" button. This file will be used in your Android project. - Click "Next" until you reach the "Firebase Console" screen. You can close this window since you already have the
google-services.json
file.
- In the left-side menu of your Firebase Console, click on "Realtime Database."
- Click on "Create database" and follow the on-screen instructions to set up the database.
- In the left-side menu, click on the gear icon (⚙️) and select "Project settings."
- Go to the "Service accounts" tab.
- Click on "Generate new private key" and then confirm by clicking on "Generate key."
- A JSON file will be downloaded to your computer. This is your
[project_name]-firebase-adminsdk.json
file, which you'll use in your Python script for authentication purposes. Keep this file safe and secure, as it contains sensitive information.
Now you have both the google-services.json
and [project_name]-firebase-adminsdk.json
files required for your Android app and Python script.
- Clone the repository:
- Open the project in Android Studio
- Add your google-services.json and [project_name]-firebase-adminsdk.json files to the app directory
- Build and run the app on your device
- Open the app on your device
- Before you can send a push notification by running the Python script, you'll need to:
- Change the source for the adminsdk by replacing [DIRECTORY LOCATION FOR YOUR FIRE-ADMINSDK JSON] with the directory location for your [project_name]-firebase-adminsdk.json file. You'll find it in app/src/ within the GitHub repo.
- Retrieve your FCM token by uncommenting the Retrieve_token() function in the LogScreen page of the app. You can find your FCM token in Android Studio's log. Copy the FCM token and replace [FCM TOKEN] with it in the test.py file.
- After running the python script, the push notification should be received and displayed in the app and firebase real time database!