v0.1
This commit is contained in:
77
nporadio2/notifications.py
Normal file
77
nporadio2/notifications.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from UserNotifications import (
|
||||
UNAuthorizationOptionAlert,
|
||||
UNMutableNotificationContent,
|
||||
UNNotificationAttachment,
|
||||
UNNotificationRequest,
|
||||
UNUserNotificationCenter,
|
||||
)
|
||||
from Foundation import NSURL
|
||||
from radiotrack import RadioTrack
|
||||
|
||||
|
||||
class UserNotification:
|
||||
def __init__(self):
|
||||
self._center = UNUserNotificationCenter.currentNotificationCenter()
|
||||
self._center.requestAuthorizationWithOptions_completionHandler_(
|
||||
UNAuthorizationOptionAlert, self._auth_callback
|
||||
)
|
||||
|
||||
def create_notification(
|
||||
self, title=None, subtitle=None, body=None, attachments=None
|
||||
):
|
||||
content = UNMutableNotificationContent.alloc().init()
|
||||
content.setTitle_(title)
|
||||
content.setSubtitle_(subtitle)
|
||||
content.setBody_(body)
|
||||
|
||||
if attachments:
|
||||
content.setAttachments_(attachments)
|
||||
|
||||
request = UNNotificationRequest.requestWithIdentifier_content_trigger_(
|
||||
"top2000_id", content, None
|
||||
)
|
||||
|
||||
self._center.addNotificationRequest_withCompletionHandler_(
|
||||
request, self._notification_callback
|
||||
)
|
||||
|
||||
content.dealloc()
|
||||
|
||||
@staticmethod
|
||||
def _auth_callback(granted, err):
|
||||
if not granted:
|
||||
print(granted, err)
|
||||
|
||||
@staticmethod
|
||||
def _notification_callback(err):
|
||||
if err:
|
||||
print(err)
|
||||
|
||||
|
||||
class SongNotification(UserNotification):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def notify_song(self, song: RadioTrack):
|
||||
|
||||
url = NSURL.URLWithString_(f"file://{song.cover_path}")
|
||||
|
||||
err = None
|
||||
atachement = (
|
||||
UNNotificationAttachment.attachmentWithIdentifier_URL_options_error_(
|
||||
"",
|
||||
url,
|
||||
None,
|
||||
err,
|
||||
)
|
||||
)[0]
|
||||
|
||||
if err:
|
||||
print(err)
|
||||
atachement = None
|
||||
|
||||
self.create_notification(
|
||||
song.title,
|
||||
song.artist,
|
||||
attachments=[atachement],
|
||||
)
|
||||
Reference in New Issue
Block a user