简体   繁体   English

Onesignal - 如何使用 python 向 android 设备发送推送通知?

[英]Onesignal - How to send a push notification to android device using python?

I am trying to send a push notifications to all subscribed users.我正在尝试向所有订阅用户发送推送通知。

Depending on onesignal api documentation , the content must be a valid language which i really don't understand!!根据onesignal api 文档,内容必须是我真的不懂的有效语言! why i should send the data like as mentioned in the documentation为什么我应该发送文档中提到的数据

contents: {"en":"English Text!}

Here is my python code:这是我的 python 代码:

import requests
import json

header = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": "Basic xxxx"
}

payload = {
            "app_id": "xxxx",
            "included_segments": ["All"],
            "contents":{"en":"notification","ru":"уведомление"}
            "data": {
                "body": "Hello my friend! we added a new post!",
                "fromProjectNumber": "xxxxxx",
                "groupKey": "myapp_grp",
                "groupMessage": "MyApp",
                "largeIcon": "https://example.com/icon.png",
                "lockScreenVisibility": 1,
                "priority": 5,
                "smallIcon": "icon",
                "title": "New post",
            }
        }

req = requests.post("https://onesignal.com/api/v1/notifications", headers=header, data=json.dumps(payload))

I am receiving the notification, but no data shows up, in fact.我正在收到通知,但实际上没有显示任何数据。 i am getting only the text in contents.我只得到内容中的文本。

Finally i recognized that the payload that i am sending is incorrect, this how it should looks like:最后我认识到我发送的有效载荷不正确,它应该是这样的:

payload = {
    "app_id": "xxxx",
    "included_segments": ["All"],
    "contents": {"en": "Lorem ipsum dolor amit falit matit", "ru": "Lorem ipsum dolor amit falit matit"},
    "android_gcm_sender_id": "620941305752",
    "android_group": "myapp_grp",
    "android_group_message": "MyApp",
    "large_icon": "https://img.onesignal.com/n/icon.png",
    "android_visibility": 1,
    "priority": 5,
    "android_sound":"notification",
    "headings": {"en": "New post", "ru": "Мы опубликовали новую статью"}
}

Also, I've found a cool python library for sending push notifications through Onesignal A Python client library for OneSignal API.另外,我发现了一个很酷的 python 库,用于通过 OneSignal 发送推送通知A Python 客户端库,用于 OneSignal API。 , this code is working as needed: ,此代码根据需要工作:

payload = {
    "included_segments": ["All"],
    "contents": {"en": "Lorem ipsum dolor amit ", "ru": "Lorem ipsum dolor amit"},
    "android_gcm_sender_id": "XXXX",
    "android_group": "myapp_grp",
    "android_group_message": "MyApp",
    "large_icon": "https://img.onesignal.com/n/icon.png",
    "android_visibility": 1,
    "priority": 5,
    "android_sound":"notification",
    "headings": {"en": "New post NazarNews TV", "ru": "Мы опубликовали новую статью"}
}

# create a onesignal client
onesignal_client = onesignal_sdk.Client(
    app_auth_key="XXXX",
    app_id="XXXX"
)

# create a notification
new_notification = onesignal_sdk.Notification(post_body=payload)

# Send notification
onesignal_response = onesignal_client.send_notification(new_notification)

But i don't know why there is no notification sound!但是不知道为什么没有提示音!

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

相关问题 如何在pythonflask或segment中向特定用户发送Onesignal推送通知? - How to send Onesignal push notification to specific user in python flask or with segment? 如何使用Python通过Firebase通过Lambda发送推送通知 - How to send push notification via Lambda by Firebase using Python 从 python 推送通知到 android - Push notification from python to android 如何向多个设备发送通知 - How to send Notification to Multiple device 如何使用 fcm_django 在 IO 上发送推送通知 - How to send push notification on IOs using fcm_django 如何使用 firebase admin sdk 向所有用户发送推送通知? - How to send a push notification to all users using firebase admin sdk? 是否可以在带有可扩展选项的推送通知中发送 Firebase(FCM) 通知消息(多行消息); 使用 Python? - Is it possible to send Firebase(FCM) notification message(multiple lined message) in the push notification with expandable option on it; using Python? 通过python向许多用户发送推送通知 - Send push notification to many users via python 如何在 Firebase 中使用 Python SDK 为 Android 发送应用内通知 | iOS - How to send In-APP Notification in Firebase using Python SDK for Android | iOS 如何通过 Python 中的 Dialogflow 实现从谷歌助手发送推送通知 - How to send push notification from google assistant through Dialogflow fulfilment in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM