Skip to main content

A Python wrapper around the OneSignal API Edit Add topics

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={"app_auth_key": "XXXX", "app_id": "YYYYY"})

You can also create a Client for multiple Apps

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXXX",
                                        apps=["id1", "id2", "id3"])

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 = {"app_auth_key": "XXXX", "app_id": "YYYYY"}

Note that, app must be a dictionary and must have “app_auth_key” and “app_id”. It will raise OneSignalError otherwise:

from onesignal_sdk.error import OneSignalError

try:
    onesignal_client = onesignal_sdk.Client()
    onesignal_client.app = {"app_auth_key": "XXXX"}
except OneSignalError as e:
    print(e)

Creating a notification

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

if you want to change contents later:

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

You can set filters, data, buttons and all of the fields available on OneSignal Documentation by using .set_parameter(name, value) method:

new_notification.set_parameter("data", {"foo": 123, "bar": "foo"})
new_notification.set_parameter("headings", {"en": "English Title"})

Sending push notification

To can send a notification to Segments:

# create a onesignal client
onesignal_client = onesignal_sdk.Client(user_auth_key="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

# create a notification
new_notification = onesignal_sdk.Notification(contents={"en": "Message"})
new_notification.set_parameter("headings", {"en": "Title"})

# set target Segments
new_notification.set_included_segments(["All"])
new_notification.set_excluded_segments(["Inactive Users"])

# 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 using Filters:

# create a notification
new_notification = onesignal_sdk.Notification(contents={"en": "Message"})
new_notification.set_parameter("headings", {"en": "Title"})

# set filters
new_notification.set_filters([
    {"field": "tag", "key": "level", "relation": ">", "value": "10"},
    {"field": "amount_spent", "relation": ">", "value": "0"}
])

# 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:

# create a notification
new_notification = onesignal_sdk.Notification(contents={"en": "Message"})
new_notification.set_parameter("headings", {"en": "Title"})

# set filters
new_notification.set_target_devices(["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())

Note that .send_notification(notification) method will send the notification to the app specified during the creation of Client object. If you want to send notification to multiple apps, you must set apps array instead:

onesignal_client.app = None
onesignal_client.apps = ["app_id_1", "app_id_2"]

Cancelling a notification

onesignal_client = onesignal_sdk.Client(user_auth_key="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

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="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

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="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

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="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

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="XXXX",
                                        app={"app_auth_key": "XXXXXX",
                                             "app_id": "XXXX-XXXX-XXX"})

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-0.1.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

onesignal_sdk-0.1.1-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for onesignal_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9acab53382d1557824410a78c7ff1f25d2dc52d65f62bd95a2cb23cdf44278a9
MD5 a4fc2052073ce0df7a5ab1c3bf4265c1
BLAKE2b-256 df91948036b188e0ba77ad0fbe101b46ca82fc299446f0b2b7fa5b8ac4d91951

See more details on using hashes here.

File details

Details for the file onesignal_sdk-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for onesignal_sdk-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cb0b4eaaa1be6ef0c52abceb8ae410e589268268b2c1302ead480baa3dd08a09
MD5 da840a67f912ccb26386a7dc2e30fa18
BLAKE2b-256 9070eee62c3678f1f40be8212d0e7946178912e6c64da62e264039fa4c484bbd

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