Skip to content

Commit

Permalink
Reformat FCM sample
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthecheung committed Oct 19, 2023
1 parent 4af0ea6 commit af1069f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions Python/fcm-notifications/functions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
print(f"User {follower_uid} is now following user {followed_uid}")
tokens_ref = db.reference(f"users/{followed_uid}/notificationTokens")
notification_tokens = tokens_ref.get()
if (
not isinstance(notification_tokens, dict)
or len(notification_tokens) < 1
):
if (not isinstance(notification_tokens, dict) or len(notification_tokens) < 1):
print("There are no tokens to send notifications to.")
return
print(
f"There are {len(notification_tokens)} tokens to send notifications to."
)
print(f"There are {len(notification_tokens)} tokens to send notifications to.")

follower: auth.UserRecord = auth.get_user(follower_uid)
notification = messaging.Notification(
Expand All @@ -44,8 +39,7 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:

# Send notifications to all tokens.
msgs = [
messaging.Message(token=token, notification=notification)
for token in notification_tokens
messaging.Message(token=token, notification=notification) for token in notification_tokens
]
batch_response: messaging.BatchResponse = messaging.send_each(msgs)
if batch_response.failure_count < 1:
Expand All @@ -54,7 +48,7 @@ def send_follower_notification(event: db_fn.Event[db_fn.Change]) -> None:
# Clean up the tokens that are not registered any more.
for response in batch_response.responses:
if response.exception.code in (
"messaging/invalid-registration-token",
"messaging/registration-token-not-registered",
"messaging/invalid-registration-token",
"messaging/registration-token-not-registered",
):
tokens_ref.child(response.message_id).delete()

0 comments on commit af1069f

Please sign in to comment.