workouttest_backoffice/aitrainer_backoffice/controlling/automation/fcm.py
Tibor Bossanyi (Freelancer) 696ce5a368 V1.30 notification
2021-10-01 15:53:20 +02:00

98 lines
3.7 KiB
Python

import datetime
from firebase_admin import messaging, initialize_app, exceptions
class FCM:
logo_url = 'https://workouttest.com/wp-content/uploads/2020/10/WT_long_logo.png'
# default constructor
def __init__(self):
# To learn more, visit the docs here:
# https://cloud.google.com/docs/authentication/getting-started>
default_app = initialize_app()
def send_to_multiple_token(self, title, body, registration_token, image_url = None):
try:
notification_image_url = image_url
#if image_url == None:
# notification_image_url = self.logo_url
message = messaging.MulticastMessage(
notification=messaging.Notification(
title=title,
body=body,
),
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
image=notification_image_url,
),
),
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(badge=1),
),
fcm_options= messaging.APNSFCMOptions(
image=notification_image_url,
)
),
tokens= registration_token,
)
response = messaging.send_multicast(message)
# Response is a message ID string.
print('Successfully sent message:', response);
except exceptions.FirebaseError as error:
print('Error sending message:', error);
except ValueError as value_error:
print('Error sending message:', value_error);
def send_to_token(self, title, body, image_url = None, registration_token = None):
if registration_token == None:
return "Registration token is null"
try:
#notification_image_url = image_url
#if image_url == None:
notification_image_url = self.logo_url
registration_token = 'cOqNt8rzo074gbIkBSpCgW:APA91bEBuNi3iVzGKb4JhxqN2j80MoJbNptLHk2qsdeKBQz5grpHtrPPXvDqn5BJVVSaj1nwGPwgN7pi6FIApog_TTP3g1yobgmgpPN6udrYgzILlVPMvdGGFDSDh6gKlczhlTL9NEp0'
print(f'image: {notification_image_url}' )
message = messaging.Message(
notification=messaging.Notification(
title=title,
body=body,
),
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
image=notification_image_url,
),
),
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(badge=1),
),
fcm_options= messaging.APNSFCMOptions(
image=notification_image_url,
)
),
token= registration_token,
)
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response);
rc = 'OK'
except exceptions.FirebaseError as error:
print('Error sending message:', error);
rc = error
except ValueError as value_error:
print('Error sending message:', value_error);
rc = value_error
return rc