workouttest_backoffice/aitrainer_backoffice/controlling/automation/fcm.py
Tibor Bossanyi (Freelancer) 3a429e714b BO 1.33 training program
2021-11-11 21:16:12 +01:00

105 lines
3.7 KiB
Python

import datetime
import os
from firebase_admin import messaging, exceptions
class FCM:
logo_url = 'https://workouttest.com/wp-content/uploads/2020/10/WT_long_logo.png'
image_base = 'https://admin.aitrainer.app/media/images/'
# default constructor
def __init__(self):
# To learn more, visit the docs here:
# https://cloud.google.com/docs/authentication/getting-started>
pass
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 = self.logo_url
''' if image_url == None or self.image_base == None:
notification_image_url = self.logo_url
else:
notification_image_url = self.image_base + '/' + image_url
'''
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
def get_message():
pass