Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def send_notification(registration_ids, message_title , message_desc):
- #* this is the API key
- fcm_api = "Your_API_KEY"
- url = "https://fcm.googleapis.com/fcm/send"
- headers = {
- "Content-Type": "application/json",
- "Authorization": 'key=' + fcm_api
- }
- payload = {
- "registration_ids": registration_ids,
- "priority": "high",
- "notification": {
- "body": message_desc,
- "title": message_title,
- "image": "https://i.ytimg.com/vi/m5WUPHRgdOA/hqdefault.jpg?sqp=-oaymwEXCOADEI4CSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLDwz-yjKEdwxvKjwMANGk5BedCOXQ",
- "icon": "https://yt3.ggpht.com/ytc/AKedOLSMvoy4DeAVkMSAuiuaBdIGKC7a5Ib75bKzKO3jHg=s900-c-k-c0x00ffffff-no-rj",
- }
- }
- result = requests.post(url, data=json.dumps(payload), headers=headers)
- return result
- ####### in any views.py you can put the next lines to send notification for many users and don't forget to import sen_notification()method
- qs = CustomUser.objects.values('id').exclude(id=request.user.id) # exclude user to send to himself
- users = [obj[0] for obj in qs]
- fcm_user = [
- (FCMDevice.objects
- .values(
- 'registration_id'
- ).filter(
- user_id=obj
- )
- ) for obj in users
- ]
- ## An error occurs by the next line when sending notification to any user
- ## that has no record in FCMDevice model SO YOU MUST MAKE ALL USERS HAVE A RECORD IN THIS TABLE
- ## TO AVOID ERROR
- reg_id = [obj[0] for obj in fcm_user]
- send_to_supplier = send_notification(
- reg_id, 'for all user New Order Created',
- str("Order Created Successfully ...")
- )
Add Comment
Please, Sign In to add comment