简体   繁体   English

如何使用 fcm_django 在 IO 上发送推送通知

[英]How to send push notification on IOs using fcm_django

I'm using this plugin to send push notifications from my Django REST app.我正在使用这个插件从我的 Django REST 应用程序发送推送通知。

https://github.com/xtrinch/fcm-django https://github.com/xtrinch/fcm-django

It works fine for the android side but IOs are unable to receive any notifications.它适用于 android 端,但 IO 无法接收任何通知。 Can anybody please tell me what I'm I missing here.谁能告诉我我在这里想念什么。

Following are my fcm_django configurations:以下是我的 fcm_django 配置:

FCM_DJANGO_SETTINGS = {
    "APP_VERBOSE_NAME": "app-name",
    "FCM_SERVER_KEY": "<firebase-server-key>",
    "ONE_DEVICE_PER_USER": True,
    "DELETE_INACTIVE_DEVICES": False,
}

Following is my code which I use to send a notification to a device:以下是我用来向设备发送通知的代码:

data = {
        "title": 'New Notification fired',
        "body": 'I just fired a new notification'}
devices.send_message(data=data)

It results in the following success response:它导致以下成功响应:

{'multicast_ids': [1212322313231212], 'success': 1, 'failure': 0, 'canonical_ids': 0, 'results': [{'message_id': '0:1579690926842318%a93f219bf9fd7ecd'}], 'topic_message_id': None}

Any help in this regard is highly appreciated.高度赞赏这方面的任何帮助。 Thank you for your time.感谢您的时间。

I have faced the same problem , there was an issue in this repo I managed to try some solution from it我遇到了同样的问题,这个 repo 中存在一个问题,我设法从中尝试了一些解决方案

this solution works good for me这个解决方案对我很有用

data = {
    "title": 'New Notification fired',
    "body": 'I just fired a new notification'
}
kwargs = {
        "content_available": True,
        'extra_kwargs': {"priority": "high", "mutable_content": True, 'notification': data },
}
for device in devices:
        if device.type == 'ios':
            device.send_message(sound='default', **kwargs)
        else:
            device.send_message(data=data)

try this I am sure it will work as I am using in all of my projects试试这个我相信它会在我所有的项目中使用

then enhance it with this然后用这个增强它

devices.objects.filter(type='ios').send_message(sound='default', **kwargs)
devices.objects.exclude(type='ios').send_message(data=data)

edit "more clarification"编辑“更多说明”

In iOS, to provide a background notification, the JSON sent to firebase MUST have a key "content_available" : true and other issue there is no sound on notification.在 iOS 中,为了提供后台通知,发送到 firebase 的 JSON 必须有一个键 "content_available" : true 和其他问题,通知没有声音。 this is my working json with sound and background notification for iOS.这是我的工作 json,带有 iOS 的声音和背景通知。

{ 
   "data":{  
      "key":"...firebaseserverkey..." 
   },
   "content_available" : true,
   "notification":{ 
       "sound": "default",
       "title": "...",
       "body":"..."
   },
 "to":"...devicetoken..." 
}

just try to send a post request with that body using postman with this url https://fcm.googleapis.com/fcm/send this will do what fcm-django do只需尝试使用邮递员与此 URL https://fcm.googleapis.com/fcm/send发送带有该正文的帖子请求,这将执行 fcm-django 的操作

content_available - On iOS, use this field to represent content-available in the APNs payload. content_available - 在 iOS 上,使用此字段表示 APNs 负载中的内容可用 When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server.当发送通知或消息并将其设置为 true 时,会唤醒不活动的客户端应用程序,并且消息通过 APNs 作为静默通知而不是通过 FCM 连接服务器发送。 Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc. On Android, data messages wake the app by default.请注意,APN 中的静默通知不能保证被传递,并且可能取决于用户开启低功耗模式、强制退出应用程序等因素。在 Android 上,数据消息默认唤醒应用程序。 On Chrome, currently not supported.在 Chrome 上,目前不支持。

priority (also from the docs): priority (也来自文档):

Sets the priority of the message.设置消息的优先级。 Valid values are "normal" and "high."有效值为“正常”和“高”。 On iOS, these correspond to APNs priorities 5 and 10.在 iOS 上,这些对应于 APNs 优先级 5 和 10。

By default, notification messages are sent with high priority, and data messages are sent with normal priority.默认情况下,通知消息以高优先级发送,数据消息以普通优先级发送。 Normal priority optimizes the client app's battery consumption and should be used unless immediate delivery is required.正常优先级可优化客户端应用程序的电池消耗,除非需要立即交付,否则应使用该优先级。 For messages with normal priority, the app may receive the message with unspecified delay.对于具有正常优先级的消息,应用程序可能会以未指定的延迟接收消息。

When a message is sent with high priority, it is sent immediately, and the app can display a notification.当一条消息以高优先级发送时,它会立即发送,并且应用程序可以显示通知。

as mentioned here Firebase messaging - whats "content_available" : true also you can read the docs for more information正如此处提到的Firebase 消息传递 - 什么是“content_available”:true您也可以阅读文档以获取更多信息

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM