[project]firebase cloud message by using python and android

Firebase

Google has provided almost free BaaS (backend as a service).
It’s very convenient for an engineer has no backend service resource.
I spent a few day to study this. It’s really easy to use.

This is my idea to use firebase.
I need a message service to inform my android app from PC side.
arch

Receiving notification message..

android_notification

python:
I use the open source to implement the part for sending message.
https://github.com/olucurious/PyFCM/

Android app can get the notification message from message_body.
The data_message is passing to app.


# Send to single device.
from pyfcm import FCMNotification
import os
import sys
import json

device_id = sys.argv[1]

server_key = "AAAA1UWsZeY:APA91bH1j..........."
push_service = FCMNotification(api_key=server_key)
registration_id = device_id
message_title = "PB PerfEval"
message_body = massge+massge2+"timestamp:"+timestamp

datamsg = {
    "data" : message_body
}
#click_action="com.precisebiometrics.perfevalmessge.TARGET_NOTIFICATION"
result = push_service.notify_single_device(registration_id=registration_id,message_title=message_title,message_body=message_body, data_message=datamsg)
print(message_body)

the server_key is from the console of cloud message.
console1

The device_id needs to get from your android devices.
One thing needs to be noted.
The device id will be changed when you uninstall the app.

The part of getting the device ID:


 String token = FirebaseInstanceId.getInstance().getToken();

The android app part is using google example code.

https://firebase.google.com/docs/cloud-messaging/android/start/

發表留言