Push notification from Python server to Android

Purpose of the article

Previous article I think I made a template for push notifications, so this time I will send the notifications from my server instead of the Firebase console. An article about trying to put it out.

The server uses CentOS7 set up on AWS. I think it doesn't matter what language you use, but I chose Python 3.7 for the time being. It is assumed that Python and pip are already installed.

1. Procedure for sending notification message

In addition to notifications, I would like the client to do various things using the information sent there as a trigger, but first I will omit that area and aim to simply send notifications.

1.1 Addition of Firebase Admin SDK

I think it's good to understand that the Firebase Admin SDK is a tool that mediates the interaction between the server and Firebase. Since Python is selected as the language to be used this time, the Firebase Admin Python SDK will be installed. Execute this according to the procedure of here. If pip is included, just execute the following one command. This time version 4.2.0 was installed.

pip install firebase-admin

1.2 Approval of send request

The reference is here, but as a comment, it is stated that "the approval of the send request is required". First, check the procedure of here.

Some patterns are described, but this time the request is issued from the server set up on AWS, so it corresponds to Downloading the JSON file of the service account from the Firebase project. The procedure corresponds to Provide credentials manually on the above page.

I don't think there is any particular clogging, just download the file and set the environment variables. Since the Admin SDK is used this time, it is not necessary to perform the steps after "Create an access token using authentication information".

1.3 Trial transmission

Return to here again. Trial transmission was carried out by diverting the sample for transmission. The code is as follows.

import firebase_admin
from firebase_admin import messaging

default_app = firebase_admin.initialize_app()

# This registration token comes from the client FCM SDKs.
registration_token = 'Device token'

myNotification = messaging.Notification(title='Test', body='Hello, FCM from My Phthon Server!')

# See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    token=registration_token,
    notification=myNotification
)

# Send a message to the device corresponding to the provided
# registration token.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

Basically, the sample script is the same, but the following are mainly changed.

--Copy the initialization process of here to respond to the error message "Initialize SDK". --Since it is necessary to pass the device token to the server side in some form, this time it is frankly dealt with as follows. --Prepare an editText view on the client side and paste the token there --Copy and paste it to create a text file --Bring the text file to the server side and write it directly in the 'device token' part of the code --If you keep the sample, nothing will be displayed and it will be lonely, so add notification. ――Since I was angry that I should pass it as an instance, I assigned the instance to myNotification and passed it. ――I haven't used data at all this time, but I left it for the time being because it is recognized as harmless.

The specifications of firebase_admin.messaging are summarized in here, so please refer to them as appropriate.

When I ran this python script, after a while, the push notification shown in Fig. 1 arrived safely on the Android device.

notification2.png Figure 1: Push notifications issued by the server

For the time being, the process of kicking from the self-made server side can be implemented.

Recommended Posts

Push notification from Python server to Android
Push notifications from Python to Android using Google's API
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Send a message from Slack to a Python server
Post from Python to Slack
Cheating from PHP to Python
Push notification to Intel Edison
Cause Android to execute processing triggered by push notification
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
ODBC access to SQL Server from Linux with Python
Create folders from '01' to '12' with python
[Lambda] [Python] Post to Twitter from Lambda!
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
How to access wikipedia from python
Python to switch from another language
Review from git init to git push
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
Terminal association from the server side to Amazon SNS (python + boto3)
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
How to update Google Sheets from Python
Send a message from Python to Slack
I want to use jar from python
Connecting from python to MySQL on CentOS 6.4
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert from Markdown to HTML in Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Try using the Python web framework Django (1)-From installation to server startup
[python] Send the image captured from the webcam to the server and save it
Edit Excel from Python to create a PivotTable
Updated to Python 2.7.9
How to open a web browser from python
ODBC connection to FileMaker 11 Server Advanced with Python 3
Study from Python Hour7: How to use classes
[Python] Convert from DICOM to PNG or CSV
Import Excel file from Python (register to DB)
I want to email from Gmail using Python.
How to use Python Kivy ④ ~ Execution on Android ~
[Python] I want to manage 7DaysToDie from Discord! 1/3
From file to graph drawing in Python. Elementary elementary
[Python] How to read data from CIFAR-10 and CIFAR-100
[python] Create table from pandas DataFrame to postgres
[Bash] Use here-documents to get python power from bash
How to generate a Python object from JSON
Sum from 1 to 10
sql from python
How to handle Linux commands well from Python
POST images from ESP32-CAM (MicroPython) to the server
Sample to send slack notification with python lambda
[Python] Flow from web scraping to data analysis
MeCab from Python
I want to use ceres solver from python