# from pyfcm import FCMNotification
from django.conf import settings
from django.contrib.auth import get_user_model
User = get_user_model()


# FCM_APIKEY = "AAAAK6Xn_h8:APA91bFl-9v5qv2sLW_SCnAyDPjSYr5mBFABb6U7TCFLRuKZcVkF8yOxJSUnQ1ds2W3JqI7eKc2Z45fsuhRJ5OowfopR-AkOlec8O3YfW5h806kVI11lv5f1wJZcypXxqxI2djKRlszC"

# ### FCM token through send notifications.
# def fcm_token_notification(user_id, title, msg):
#     user = User.objects.filter(id=user_id).last()
#     push_service = FCMNotification(api_key=FCM_APIKEY)
#     registration_id = f'{user.fcm_token}'
#     message_title = title
#     message_body = msg
#     result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title, message_body=message_body)
## Install request module by running ->
#  pip3 install requests

# Replace the deviceToken key with the device Token for which you want to send push notification.
# Replace serverToken key with your serverKey from Firebase Console

# Run script by ->
# python3 fcm_python.py


import requests
import json

serverToken = 'AAAAK6Xn_h8:APA91bFl-9v5qv2sLW_SCnAyDPjSYr5mBFABb6U7TCFLRuKZcVkF8yOxJSUnQ1ds2W3JqI7eKc2Z45fsuhRJ5OowfopR-AkOlec8O3YfW5h806kVI11lv5f1wJZcypXxqxI2djKRlszC'

def send_push_notification(user_id, title, msg):
    user = User.objects.filter(id=user_id).last()
    #push_service = FCMNotification(api_key=FCM_APIKEY)
    registration_id = f'{user.fcm_token}'
    deviceToken = registration_id
    message_title = title
    message_body = msg
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'key=' + serverToken,
      }

    body = {
          'data': {'title': title,
                            'body': msg
                            },
          'to':
              deviceToken,
          'priority': 'high',
        #   'data': dataPayLoad,
        }
    response = requests.post("https://fcm.googleapis.com/fcm/send",headers = headers, data=json.dumps(body))
    print(response.status_code)

    print(response.json())





