简体   繁体   English

通过 firebase 云消息发送通知 django

[英]Send notification by firebase cloud messaging on a topic by django

I have implemented a firebase cloud messaging app which sends a notification to an android device based on its device token which works fine.我已经实现了一个 firebase 云消息传递应用程序,它根据工作正常的设备令牌向 android 设备发送通知。 I've used django-fcm module.我用过 django-fcm 模块。 But, storing device registration code for each user device is a little difficult in the practical application as I've to modify the front end source code which I don't want to.但是,在实际应用中为每个用户设备存储设备注册码有点困难,因为我必须修改我不想修改的前端源代码。

Therefor I decided to go with topic messaging.因此,我决定使用主题消息传递 go。 I cannot find any useful documentation regarding this.我找不到任何有用的文档。

view.py视图.py

def send_notification(registration_id,message_title, message_body):
   try:
      push_service = FCMNotification(api_key=FCM_DJANGO_SETTINGS['FCM_SERVER_KEY'])

      result=push_service.notify_single_device(registration_id=registration_id, message_title=message_title,
                                        message_body=message_body)
      return result
   except:
      pass

def send_noti(request):
    device_token = "dEMspOwATpiFVumQGi1QOS:APA91bEZsTu7SbRTYRDGJjhNhHRErYd_UHs43rrPY6uN4yXe5_UHnPpepocFP60wnnU2IImCBXcem0rVuEUj7PCPc9EfkC0W4cLrNSmBCoWM5mz8jp9YgYF-VurJ1JyoRH627IH5Ujxn"
    send_notification(device_token,"title","body")
    return JsonResponse({"status": "success"}, safe=False)

urls.py网址.py

urlpatterns = [
    path('noti/', views.send_noti, name='send_noti'),
]

Can anyone help to send notification on topic using django?任何人都可以帮助使用 django 发送有关主题的通知吗?

Thanks FrankvanPuffelen for your help感谢 FrankvanPuffelen 的帮助

The complete implementation is as follows.完整的实现如下。

views.py视图.py

from fcm_django.fcm import fcm_send_topic_message从 fcm_django.fcm 导入 fcm_send_topic_message

def send_noti_topic(self):
    fcm_send_topic_message(topic_name='NEWYORK_WEATHER', message_body='message body', message_title='title')
    return JsonResponse({"status": "success"}, safe=False)

urls.py网址.py

urlpatterns = [
    path('noti/', views.send_noti_topic, name='send_noti_topic'),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase 云消息推送通知 - firebase cloud messaging push notification 用于主题消息的Firebase云消息传递InvalidRegistration - Firebase Cloud Messaging InvalidRegistration for Topic Messages 无法接收来自 Firebase 云消息传递的通知 - cannot receive notification from Firebase Cloud Messaging 如何使用AppEngine Python通过Firebase Cloud Messaging将通知发送到Android应用程序 - How to send Notifications to Android App with Firebase Cloud Messaging with AppEngine Python Firebase云消息传递问题 - Firebase Cloud Messaging Issue MismatchSenderId错误Firebase云消息传递(FCM) - MismatchSenderId error Firebase Cloud Messaging (FCM) Firebase 云消息传递 HTTP v1 发送错误 - 400 客户端错误:对 url 的错误请求:https://fcm.googleapis.com/v1/projects/<PROJECT_ID> /消息:发送 - Firebase cloud messaging HTTP v1 send error - 400 Client Error: Bad Request for url: https://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send 如何为 Firebase 云消息传递获取 OAuth2.0 令牌 - How to get OAuth2.0 Token for Firebase Cloud Messaging 使用pyFCM的Python中的Firebase云消息传递(FCM) - 客户端注册 - Firebase Cloud Messaging (FCM) in Python with pyFCM - Client registration Firebase 云消息多播在令牌无效时不抛出异常 - Firebase Cloud Messaging Multicast not throwing exceptions when tokens are invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM