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.
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.
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
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".
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.
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