-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac09e67
commit 4d1240d
Showing
26 changed files
with
604 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
Receive remote notifications | ||
This module has everything needed to receive push notifications from a server. | ||
""" | ||
|
||
from pyto import __Class__ | ||
from time import sleep | ||
from typing import Dict | ||
|
||
|
||
__RemoteNotifications__ = __Class__("RemoteNotifications") | ||
|
||
|
||
def remove_category(category: str): | ||
""" | ||
Removes a category with its given identifier. | ||
:param category: The identifier of the category to remove. | ||
""" | ||
|
||
__RemoteNotifications__.removeCategory(category) | ||
|
||
|
||
def add_category(id: str, actions: Dict[str, str]): | ||
""" | ||
Adds a category of notification. | ||
A category contains set of actions for a certain type of notifications. | ||
:param id: The unique identifier of the category. | ||
:param actions: A dictionary with actions displayed by the notification. | ||
The actions are in a dictionary. Each key is the name of an action and its value is a URL that will be opened when the action is pressed. | ||
The ``"{}"`` characters on URLs will be replaced by a percent encoded data sent by the server. | ||
Example: | ||
.. highlight:: python | ||
.. code-block:: python | ||
import remote_notifications as rn | ||
actions = { | ||
"Google": "https://www.google.com/search?q={}" | ||
} | ||
rn.add_category("google", actions) | ||
In the example above, if a notification with the category id `"google"` is received, an action will be added to the notification. | ||
When it's pressed, Pyto will search on Google for the data passed by the server. | ||
""" | ||
|
||
__RemoteNotifications__.addCategory(id, actions=actions) | ||
|
||
|
||
def register() -> str: | ||
""" | ||
Registers the device for push notifications. | ||
Returns a token to use with the api. | ||
:rtype: str | ||
""" | ||
|
||
__RemoteNotifications__.register() | ||
|
||
while True: | ||
|
||
if __RemoteNotifications__.error is not None: | ||
raise RuntimeError(str(__RemoteNotifications__.error)) | ||
|
||
if __RemoteNotifications__.deviceToken is not None: | ||
return str(__RemoteNotifications__.deviceToken) | ||
|
||
sleep(0.2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// RemoteNotifications.swift | ||
// Pyto | ||
// | ||
// Created by Adrian Labbé on 02-05-20. | ||
// Copyright © 2020 Adrian Labbé. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import UserNotifications | ||
|
||
@objc class RemoteNotifications: NSObject { | ||
|
||
@objc static var deviceToken: String? | ||
|
||
@objc static var error: String? | ||
|
||
@objc static func removeCategory(_ id: String) { | ||
DispatchQueue.main.async { | ||
UNUserNotificationCenter.current().getNotificationCategories { categories in | ||
|
||
var new = [UNNotificationCategory]() | ||
|
||
for category in categories { | ||
if category.identifier != id { | ||
new.append(category) | ||
} | ||
} | ||
|
||
UNUserNotificationCenter.current().setNotificationCategories(Set(new)) | ||
} | ||
} | ||
} | ||
|
||
@objc static func addCategory(_ id: String, actions: [String:String]) { | ||
UNUserNotificationCenter.current().getNotificationCategories { categories in | ||
|
||
var notificationActions = [UNNotificationAction]() | ||
|
||
for action in actions { | ||
notificationActions.append(UNNotificationAction(identifier: action.value, title: action.key, options: .foreground)) | ||
} | ||
|
||
var new = [UNNotificationCategory]() | ||
|
||
for category in categories { | ||
if category.identifier != id { | ||
new.append(category) | ||
} | ||
} | ||
|
||
let category = UNNotificationCategory(identifier: id, actions: notificationActions, intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: []) | ||
|
||
new.append(category) | ||
|
||
UNUserNotificationCenter.current().setNotificationCategories(Set(new)) | ||
} | ||
} | ||
|
||
@objc static func register() { | ||
DispatchQueue.main.async { | ||
UNUserNotificationCenter.current().getNotificationSettings { (settings) in | ||
if settings.authorizationStatus == .denied { | ||
RemoteNotifications.error = "Notifications are disabled." | ||
} else if settings.authorizationStatus == .notDetermined { | ||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (success, error) in | ||
RemoteNotifications.error = error?.localizedDescription | ||
if !success && error == nil { | ||
RemoteNotifications.error = "Notifications are disabled." | ||
} | ||
|
||
if RemoteNotifications.error == nil { | ||
DispatchQueue.main.async { | ||
UIApplication.shared.registerForRemoteNotifications() | ||
} | ||
} | ||
} | ||
} else { | ||
DispatchQueue.main.async { | ||
UIApplication.shared.registerForRemoteNotifications() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
remote_notifications | ||
==================== | ||
|
||
.. automodule:: remote_notifications | ||
:members: | ||
|
||
|
||
Sending notifications | ||
--------------------- | ||
|
||
To send a notification, first you need a token for the device that will receive the notification. Use the :func:`~remote_notifications.register` function. | ||
|
||
The API endpoint to send remote notifications is ``https://push.pyto.app`` | ||
|
||
Parameters | ||
********** | ||
|
||
To send a notification, construct a POST request with the following parameters. | ||
|
||
:token: The token generated by the device that will receive the notification. (Required) | ||
:title: The title of the notification. (Optional) | ||
:message: The body of the notification. (Optional) | ||
:category: The category of the notification. See :func:`~remote_notifications.add_category`. (Optional) | ||
:data: Additional data passed to action URLs. See :func:`~remote_notifications.add_category`. (Optional) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.