Skip to main content

A Python wrapper around the OneSignal API

Project description

A Python client library for OneSignal API.

Table of Contents

Installation

pip install onesignal_sdk

Usage

import onesignal as onesignal_sdk

Creating a client

You can create a OneSignal Client as shown below. You can find your user_auth_key and REST API Key (app_auth_key) on OneSignal Account & API Keys page.

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

You can always create a Client with no credential and set them later:

onesignal_client = onesignal_sdk.Client()
onesignal_client.user_auth_key = "XXXXX"
onesignal_client.app_auth_key = "XXXXX"
onesignal_client.app_id = "APPID"

Creating a notification

new_notification = onesignal_sdk.Notification(post_body={"contents": {"en": "Message", "tr": "Mesaj"}})

if you want to change contents later:

new_notification = onesignal_sdk.Notification(post_body={"contents": {"en": "Message", "tr": "Mesaj"}})
...
...
new_notification.post_body["content"] = {"en": "New message"}

You can set filters, data, buttons and all of the fields available on OneSignal Documentation by updating post_body of notification:

new_notification.post_body["data"] = {"foo": 123, "bar": "foo"}
new_notification.post_body["headings"] = {"en": "English Title"}
new_notification.post_body["included_segments"] = ["Active Users", "Inactive Users"]
new_notification.post_body["filters"] = [
    {"field": "tag", "key": "level", "relation": "=", "value": "10"},
    {"operator": "OR"}, {"field": "tag", "key": "level", "relation": "=", "value": "20"}
]

Sending push notification

To can send a notification to Segments:

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

# create a notification
new_notification = onesignal_sdk.Notification(post_body={
    "contents": {"en": "Message", "tr": "Mesaj"},
    "included_segments": ["Active Users"],
    "filters": [{"field": "tag", "key": "level", "relation": "=", "value": "10"}]
})

# send notification, it will return a response
onesignal_response = onesignal_client.send_notification(new_notification)
print(onesignal_response.status_code)
print(onesignal_response.json())

To send a notification to specific devices:

onesignal_client = onesignal_sdk.Client(app_auth_key="XXXX", app_id="APPID")
new_notification = onesignal_sdk.Notification(post_body={
    "contents": {"en": "Message"},
    "include_player_ids": ["id1", "id2"],
})

# send notification, it will return a response
onesignal_response = onesignal_client.send_notification(new_notification)
print(onesignal_response.status_code)
print(onesignal_response.json())

Cancelling a notification

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

onesignal_response = onesignal_client.cancel_notification("notification_id")
print(onesignal_response.status_code)
print(onesignal_response.json())

Viewing push notifications

onesignal_response = onesignal_client.view_notifications(query={"limit": 30, "offset": 0})
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

Viewing a push notification

onesignal_response = onesignal_client.view_notification("notification_id")
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

Viewing apps

onesignal_response = onesignal_client.view_apps()

You can also view a single app:

onesignal_response = onesignal_client.view_app("app_id")

Creating an app

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

app_body = {
    "name": "Test App",
    "apns_env": "production"
}

onesignal_response = onesignal_client.create_app(app_body)
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

Updating an app

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

app_body = {
    "name": "New App",
    "gcm_key": "XX-XXX-XXXXX"
}

onesignal_response = onesignal_client.update_app(app_id="XXXX", app_body=app_body)
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

Viewing devices

onesignal_response = onesignal_client.view_devices(query={"limit": 20})
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

You can also view a device:

onesignal_response = onesignal_client.view_device("device_id")

Adding a device

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

device_body = {
    "device_type": 1,
    "language": "tr"
}

onesignal_response = onesignal_client.create_device(device_body=device_body)

Editing a device

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                    app_auth_key="XXXX",
                                    app_id="APPID")

device_body = {
    "device_type": 1,
    "language": "en"
}

onesignal_response = onesignal_client.update_device(device_id="device_id", device_body=device_body)

CSV Export

onesignal_response = onesignal_client.csv_export(post_body={"extra_fields": ["location"]})
if onesignal_response.status_code == 200:
    print(onesignal_response.json())

Opening track

onesignal_response = onesignal_client.track_open("notification_id", track_body={"opened": True})

Licence

This project is under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onesignal_sdk-1.0.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

onesignal_sdk-1.0.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file onesignal_sdk-1.0.0.tar.gz.

File metadata

  • Download URL: onesignal_sdk-1.0.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for onesignal_sdk-1.0.0.tar.gz
Algorithm Hash digest
SHA256 45a5d9a41c20ee27afff5a33ac1a4631bae50b0b25646103e0bcab4e22dfa863
MD5 df05e0c4eee5e3fa7bac608a7ed5afb2
BLAKE2b-256 167c8668b0ce2211230d5ad9e2fa5b58ad7b9c233076aca209bc2c6169347469

See more details on using hashes here.

File details

Details for the file onesignal_sdk-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: onesignal_sdk-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for onesignal_sdk-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f4e45b8486e0d12227ffe919ef8f6da9c150aa240e441089e2207f8983ae26e
MD5 35888733b715df8eb675fac9cfb3d180
BLAKE2b-256 a82125e77350cbdaac031d1c2d1c516486905f19439655e91eeb2c000ee65336

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page