def handle(self, *args, **options): # noqa: C901 """Send Push notifications.""" webpush_sent = 0 webpush_total = 0 fcm_sent = 0 push_service = FCMNotification(api_key=settings.FCM_SERVER_KEY) # Iterate all unsent notifications for notification in Notification.objects.filter(emailed=False)[:1000]: # Check invalid subscriptions if not notification or not notification.actor: continue # Get the user's profile profile = notification.recipient.profile # Check bad users if not profile: continue # Stop the spam! notification.emailed = True notification.save() # Get rich notification data_message = get_rich_notification(notification) # Retro method for transition if profile.fcm_id and profile.fcm_id != '': send_fcm_notification_message(push_service, profile.fcm_id, data_message) # Send FCM push notification for device in profile.devices.all(): fcm_sent += send_notification_fcm(push_service, device, data_message) # For each web push subscription for subscription in profile.web_push_subscriptions.all(): webpush_total += 1 webpush_sent += send_notification_webpush( subscription, data_message) print("WebPush:", webpush_sent, "WebPush FAIL:", webpush_total - webpush_sent, "FCM:", fcm_sent) self.stdout.write( self.style.SUCCESS( 'Push notification chore completed successfully'))
def push_notify(pk): """Push notify a notification.""" setUp() notification = Notification.objects.filter(id=pk).first() # Check invalid subscriptions if not notification or notification.emailed or not notification.actor: return # Get the user's profile profile = notification.recipient.profile # Check bad users if not profile: return # Stop the spam! notification.emailed = True notification.save() # Get rich notification data_message = get_rich_notification(notification) # Get the API endpoint if not hasattr(settings, 'FCM_SERVER_KEY'): return push_service = FCMNotification(api_key=settings.FCM_SERVER_KEY) # Send FCM push notification for device in profile.devices.all(): send_notification_fcm(push_service, device, data_message) # For each web push subscription for subscription in profile.web_push_subscriptions.all(): send_notification_webpush(subscription, data_message)
def handle(self, *args, **options): # Initiate connection push_service = FCMNotification(api_key=settings.FCM_SERVER_KEY) # Maintain a count count = 0 # Iterate all upcoming events for event in Event.objects.filter( start_time__range=(timezone.now(), timezone.now() + timedelta(minutes=30)), starting_notified=False): # Stop the spam! event.starting_notified = True event.notify = False event.save() # Event About to Start print('EATS -', event.name) # Construct object data_message = { "type": "event", "id": str(event.id), "title": event.name, "verb": "Event is about to start", "large_icon": event.bodies.first().image_url, "image_url": event.image_url } # Notify all followers for profile in event.followers.all(): # Send FCM push notification for device in profile.devices.all(): count += send_notification_fcm(push_service, device, data_message) print('Sent', count, 'rich notifications') self.stdout.write( self.style.SUCCESS('Event starting chore completed successfully'))