· 3 years ago · Dec 23, 2021, 10:20 AM
1def send_notification(registration_ids, message_title , message_desc):
2 #* this is the API key
3 fcm_api = "AAAAwsq2zII:APA91bGINxDLrjZoCO6gDGT28h0sTrUsp5eImDujneeYXOahq74IAJaHw6j07eN6fwwPaOJg9gDuntLCWnN0ezkHblarHpH_F_xMKsZ3PYgWwYoenqchXHCwdMieM109X1WOij8p-7uQ"
4
5 url = "https://fcm.googleapis.com/fcm/send"
6
7 headers = {
8 "Content-Type": "application/json",
9 "Authorization": 'key=' + fcm_api
10 }
11
12 payload = {
13 "registration_ids": registration_ids,
14 "priority": "high",
15 "notification": {
16 "body": message_desc,
17 "title": message_title,
18 "image": "https://i.ytimg.com/vi/m5WUPHRgdOA/hqdefault.jpg?sqp=-oaymwEXCOADEI4CSFryq4qpAwkIARUAAIhCGAE=&rs=AOn4CLDwz-yjKEdwxvKjwMANGk5BedCOXQ",
19 "icon": "https://yt3.ggpht.com/ytc/AKedOLSMvoy4DeAVkMSAuiuaBdIGKC7a5Ib75bKzKO3jHg=s900-c-k-c0x00ffffff-no-rj",
20
21 }
22 }
23
24 result = requests.post(url, data=json.dumps(payload), headers=headers)
25 return result
26
27
28## here you will make a form to enable sending message
29def send(request):
30 qs = CustomUser.objects.exclude(id=request.user.id)
31 fcm_obj = FCMDevice.objects.values('registration_id') #.get(user_id=)
32 reg_id = [obj['registration_id'] for obj in fcm_obj]
33 print('reg_id= ',reg_id)
34 # registration = [
35 # ## for Hima
36 # 'dEjrMIdzPq0:APA91bFSuG2u05tI9SdrzvE4cinH3uVpX9-OyXbdEtuAtSuucqlIJEuQVZaJWzQDBKYOapDjRBprrv55SwxVTbOa01GHiSJDIJ2OTBlXf60_H_QADFNYzRVAZWdavRCYsCuFkU2cv4wX',
37 # ## for mine
38 # 'f4OJgFR6B77SunSCCueJyk:APA91bG6S2a6L6NcwA73ruBq7yHos-sZ186vaFXQhNAxbOPOOBsWB32GbcwvjdXUVzrXrD_mbr3gamsHCWwCQ0aWjZTk-wa1l3pfVSfooQ0UXkgfrEBBD31pkTmzD8zcqonAC4k4JxMh',
39 # ]
40 # check_fcm = [
41 # FCMDevice.objects.filter(
42 # registration_id=x
43 # ).exists() for x in registration
44 # ]
45
46 # match_fcm = FCMDevice.objects.filter(
47 # registration_id=registration,
48 # ).exists()
49
50 # print('check_fcm= ', check_fcm,)
51 if request.method == 'POST':
52 name = request.POST.get('name')
53 if name == "send_message":
54 title = request.POST.get('title')
55 body = request.POST.get('body')
56 print(
57 f'title ==> {title} ... Body ==> {body}'
58 )
59 users_ids = [
60 int(box[9:]) for box in request.POST.keys() if box.startswith("selected_")
61 ]
62 print(f'====> These are the user(s) id(s): {users_ids}')
63 if (title == '' or body == '') :
64 messages.info(request, 'Title and/or Body of the message can\'t be empty !!! ')
65 return redirect(reverse('send'))
66 else:
67 ## next lines for save one or more user_id in NotificationData Model
68 notification_instance = [
69 NotificationData.objects.create(
70 user_id=request.user.id, receiver_id=obj,
71 title_en=title, body_en=body,
72 title_ar=title, body_ar=body,
73 updated_user_id=request.user.id
74 ) for obj in users_ids
75 ]
76
77 send_notification(
78 reg_id, title, body
79 )
80 # print('True')
81 return redirect(reverse('send'))
82
83 # return HttpResponse("sent")
84 context = {
85 'qs': qs,
86 }
87 return render(request, 'notification/send.html', context)