【问题标题】:How to send push notification on IOs using fcm_django如何使用 fcm_django 在 IO 上发送推送通知
【发布时间】:2020-01-22 11:22:14
【问题描述】:

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

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

它适用于 android 端,但 IO 无法接收任何通知。谁能告诉我我在这里想念什么。

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

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

【问题讨论】:

    标签: python django-rest-framework firebase-cloud-messaging apple-push-notifications django-push-notifications


    【解决方案1】:

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

    这个解决方案对我很有效

    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..." 
    }
    

    只需尝试使用带有此 URL https://fcm.googleapis.com/fcm/send 的邮递员向该正文发送一个帖子请求 这将做 fcm-django 做的事情

    content_available - 在 iOS 上,使用此字段表示 APNs 负载中的 content-available。当发送通知或消息并将其设置为 true 时,将唤醒非活动客户端应用程序,并通过 APNs 作为静默通知发送消息,而不是通过 FCM 连接服务器。请注意,APN 中的静默通知不保证会传递,并且可能取决于用户打开低功耗模式、强制退出应用程序等因素。在 Android 上,默认情况下数据消息会唤醒应用程序。在 Chrome 上,目前不支持。

    priority(也来自文档):

    设置消息的优先级。有效值为“正常”和“高”。在 iOS 上,这些对应于 APN 优先级 5 和 10。

    默认情况下,通知消息以高优先级发送,数据消息以普通优先级发送。正常优先级可优化客户端应用程序的电池消耗,除非需要立即交付,否则应使用此优先级。对于具有正常优先级的消息,应用可能会以未指定的延迟接收消息。

    高优先级的消息发送后,立即发送,应用可以显示通知。

    这里提到Firebase messaging - whats "content_available" : true 您也可以阅读文档以获取更多信息

    【讨论】:

    • 感谢您的宝贵时间。您能否解释一下为什么我们需要发送这些额外的标志,例如“content_available”和“mutable_content”?我知道 Android 可以更好地接收数据对象作为通知,而 IO 可以接收通知对象作为通知。
    • 我将编辑我的答案,但我首先需要知道它是否对您有用,但简而言之,发送功能真正做的是使用服务器密钥和设备令牌进行发布请求以触发 base , 在 fire base docs 中很明显,当发送到 ios 设备时,您应该用通知键替换数据键,我所做的更改删除了默认行为发送为有效的行为,我将编辑答案以进行更多说明
    • 我已经编辑了我的答案,也是制作 fcm 通知的好资源,您可以使用github.com/jazzband/django-push-notifications
    • 该解决方案对我有用。感谢您的时间。真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多