Change Pushy to OneSignal for push notifications

This commit is contained in:
JonnyWong16 2017-03-28 09:38:03 -07:00
parent c405f04e9c
commit 42a895b095
3 changed files with 39 additions and 28 deletions

View file

@ -537,7 +537,7 @@ def dbcheck():
# mobile_devices table :: This table keeps record of devices linked with the mobile app
c_db.execute(
'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, '
'device_token TEXT NOT NULL UNIQUE, device_name TEXT, friendly_name TEXT)'
'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT)'
)
# Upgrade sessions table from earlier versions
@ -1039,6 +1039,22 @@ def dbcheck():
except:
pass
# Upgrade mobile_devices table from earlier versions
try:
result = c_db.execute('SELECT SQL FROM sqlite_master WHERE type="table" AND name="mobile_devices"').fetchone()
if 'device_token TEXT NOT NULL UNIQUE' in result[0]:
logger.debug(u"Altering database. Dropping and recreating mobile_devices table.")
c_db.execute(
'DROP TABLE mobile_devices'
)
c_db.execute(
'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, '
'device_id TEXT NOT NULL UNIQUE, device_token TEXT, device_name TEXT, friendly_name TEXT)'
)
except sqlite3.OperationalError:
logger.warn(u"Failed to recreate mobile_devices table.")
pass
# Add "Local" user to database as default unauthenticated user.
result = c_db.execute('SELECT id FROM users WHERE username = "Local"')
if not result.fetchone():

View file

@ -341,13 +341,13 @@ class API2:
return data
def register_device(self, device_name='', device_token='', **kwargs):
def register_device(self, device_name='', device_id='', **kwargs):
""" Registers the PlexPy Android App for notifications.
```
Required parameters:
device_token (str): The device token of the PlexPy Android App
device_name (str): The device name of the PlexPy Android App
device_id (str): The OneSignal device id of the PlexPy Android App
Optional parameters:
None
@ -361,14 +361,14 @@ class API2:
self._api_result_type = 'error'
return
elif not device_token:
elif not device_id:
self._api_msg = 'Device registartion failed: no device token provided.'
self._api_result_type = 'error'
return
db = database.MonitorDatabase()
keys = {'device_token': device_token}
keys = {'device_id': device_id}
values = {'device_name': device_name}
try:

View file

@ -665,28 +665,28 @@ class ANDROIDAPP(Notifier):
"""
PlexPy Android app notifications
"""
_DEFAULT_CONFIG = {'api_key': '',
'device_token': ''
_DEFAULT_CONFIG = {'device_id': ''
}
ONESIGNAL_APP_ID = '3b4b666a-d557-4b92-acdf-e2c8c4b95357'
def notify(self, subject='', body='', action='', **kwargs):
if not subject or not body:
return
data = {'to': self.config['device_token'],
'data': {'subject': subject.encode("utf-8"),
'body': body.encode("utf-8")}
data = {'app_id': self.ONESIGNAL_APP_ID,
'include_player_ids': [self.config['device_id']],
'headings': {'en': subject.encode("utf-8")},
'contents': {'en': body.encode("utf-8")}
}
logger.debug(u"PlexPy Notifiers :: Pushy request body: %s" % json.dumps(data))
http_handler = HTTPSConnection("api.pushy.me")
http_handler = HTTPSConnection("onesignal.com")
http_handler.request("POST",
"/push?%s" % urlencode({'api_key': self.config['api_key']}),
"/api/v1/notifications",
headers={'Content-type': "application/json"},
body=json.dumps(data))
response = http_handler.getresponse()
request_status = response.status
logger.debug(u"PlexPy Notifiers :: Pushy response: %s" % response.read())
if request_status == 200:
logger.info(u"PlexPy Notifiers :: Android app notification sent.")
@ -711,9 +711,9 @@ class ANDROIDAPP(Notifier):
devices = {}
for device in result:
if device['friendly_name']:
devices[device['device_token']] = device['friendly_name']
devices[device['device_id']] = device['friendly_name']
else:
devices[device['device_token']] = device['device_name']
devices[device['device_id']] = device['device_name']
return devices
@ -724,26 +724,21 @@ class ANDROIDAPP(Notifier):
devices_config = {'label': 'Device',
'description': 'No devices registered. ' \
'<a data-tab-destination="tabs-android_app" data-toggle="tab" data-dismiss="modal" ' \
'style="cursor: pointer;">Register the Android app with PlexPy.</a>',
'style="cursor: pointer;">Click here</a> to get the Android App.',
'input_type': 'help'
}
else:
devices_config = {'label': 'Device',
'value': self.config['device_token'],
'name': 'androidapp_device_token',
'description': 'Set your Android app device.',
'value': self.config['device_id'],
'name': 'androidapp_device_id',
'description': 'Set your Android app device or ' \
'<a data-tab-destination="tabs-android_app" data-toggle="tab" data-dismiss="modal" ' \
'style="cursor: pointer;">register a new device</a> with PlexPy.',
'input_type': 'select',
'select_options': devices
}
config_option = [{'label': 'Pushy API Key',
'value': self.config['api_key'],
'name': 'androidapp_api_key',
'description': 'Your Pushy API key.',
'input_type': 'text'
},
devices_config
]
config_option = [devices_config]
return config_option