我已经按照步骤操作了。
pip install fcm-django
编辑 settings.py 文件
INSTALLED_APPS = ("fcm_django")
FCM_DJANGO_SETTINGS = {
"FCM_SERVER_KEY": "[your api key]"
}
然后
- manage.py 迁移
如何在令牌上发送推送消息?我不明白以下方法
from fcm_django.models import FCMDevice
device = FCMDevice.objects.all().first()
device.send_message("Title", "Message")
device.send_message(data={"test": "test"})
device.send_message(title="Title", body="Message", icon=..., data={"test": "test"})
pip install pyfcm
或
pip install git+https://github.com/olucurious/PyFCM.git
# Send to single device.
from pyfcm import FCMNotification
push_service = FCMNotification(api_key="<api-key>")
registration_id = "<device registration_id>"
message_title = "Uber update"
message_body = "Hi john, your customized news for today is ready"
result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
print result
或
# Send to multiple devices by passing a list of ids.
registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)
print result
在谷歌自己得到这个结果并它工作以获取更多信息后,请使用此链接 https://pypi.org/project/pyfcm/0.0.4/
它们是发送消息或通知的方法。 每当必须发送消息时,都必须在视图中调用这些方法。
您可以创建一个通用方法
from fcm_django.models import FCMDevice
def send_notification(user_id, title, message, data):
try:
device = FCMDevice.objects.filter(user=user_id).last()
result = device.send_message(title=title, body=message, data=data,
sound=True)
return result
except:
pass
并在必须发送消息或通知时添加调用此方法。
send_notification(user_id=1, title="Order Return", message="your
message", data=None)