[英]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.