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

How to send push notification on IOs using fcm_django

我正在使用这个插件从我的 Django REST 应用程序发送推送通知。

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

android 端工作正常,但 IOs 无法收到任何通知。谁能告诉我我在这里想念的是什么。

以下是我的 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,
}

以下是我用来向设备发送通知的代码:

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

它会导致以下成功响应:

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

非常感谢这方面的任何帮助。感谢您的时间。

我遇到了同样的问题, 这个回购中有一个问题我设法从中尝试了一些解决方案

这个解决方案对我很有效

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)

试试这个我相信它会像我在我所有项目中使用的那样工作

然后用这个

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

编辑“更多说明”

在 iOS 中,要提供后台通知,发送到 firebase 的 JSON 必须有一个键“content_available”:true 和其他问题通知没有声音。 这是我的工作 json,带有 iOS.

的声音和背景通知
{ 
   "data":{  
      "key":"...firebaseserverkey..." 
   },
   "content_available" : true,
   "notification":{ 
       "sound": "default",
       "title": "...",
       "body":"..."
   },
 "to":"...devicetoken..." 
}

尝试用 postman 和这个 url https://fcm.googleapis.com/fcm/send 发送一个 post 请求 这将做 fcm-django 做的事情

content_available - On iOS, use this field to represent content-available in the APNs payload. 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. 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. On Chrome, currently not supported.

priority(也来自文档):

Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 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.

如此处所述 您也可以阅读文档以获取更多信息