FuTChY

send_fcm.sh

Feb 27th, 2025 (edited)
72
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.55 KB | Software | 0 0
  1. #!/bin/bash
  2. # send_fcm.sh
  3. # Usage: ./send_fcm.sh "
  4. # This is just some sort of conversion of this page:
  5. # https://tasker.joaoapps.com/tests/remote%20task%20execution%20example.html made by João Dias the owner of Tasker.
  6.  
  7. SERVICE_ACCOUNT_FILE="PUT YOUR FILE PATH HERE"
  8. GOOGLE_SERVICES_FILE="PUT YOUR FILE PATH HERE"
  9. DEVICE_TOKEN="THE TOKEN FOR THE REMOTE DEVICE"
  10. TEXT_TO_SHOW="Hi, this is just a test."
  11.  
  12. # Check if required commands are available: jq, openssl, curl
  13. for cmd in jq openssl curl; do
  14.     if ! command -v "$cmd" &> /dev/null; then
  15.         echo "Error: $cmd is required but not installed."
  16.         exit 1
  17.     fi
  18. done
  19.  
  20. # Extract required fields from the service account file
  21. CLIENT_EMAIL=$(jq -r '.client_email' "$SERVICE_ACCOUNT_FILE")
  22. PRIVATE_KEY=$(jq -r '.private_key' "$SERVICE_ACCOUNT_FILE")
  23. if [ -z "$CLIENT_EMAIL" ] || [ -z "$PRIVATE_KEY" ]; then
  24.     echo "Error: Could not extract client_email or private_key from $SERVICE_ACCOUNT_FILE"
  25.     exit 1
  26. fi
  27.  
  28. # Extract project ID from the google services file
  29. PROJECT_ID=$(jq -r '.project_info.project_id' "$GOOGLE_SERVICES_FILE")
  30. if [ -z "$PROJECT_ID" ]; then
  31.     echo "Error: Could not extract project_id from $GOOGLE_SERVICES_FILE"
  32.     exit 1
  33. fi
  34.  
  35. # Function for Base64 URL-safe encoding (no padding, '+' -> '-', '/' -> '_')
  36. base64url_encode() {
  37.     openssl base64 -e -A | tr '+/' '-_' | tr -d '='
  38. }
  39.  
  40. # Generate JWT Header and Payload
  41. header='{"alg":"RS256","typ":"JWT"}'
  42. iat=$(date +%s)
  43. exp=$((iat + 3600))
  44. scope="https://www.googleapis.com/auth/firebase.messaging"
  45. aud="https://oauth2.googleapis.com/token"
  46.  
  47. payload=$(jq -n \
  48.     --arg iss "$CLIENT_EMAIL" \
  49.     --arg scope "$scope" \
  50.     --arg aud "$aud" \
  51.     --argjson iat "$iat" \
  52.     --argjson exp "$exp" \
  53.     '{iss: $iss, scope: $scope, aud: $aud, iat: $iat, exp: $exp}')
  54.  
  55. # Base64url encode header and payload
  56. header_enc=$(echo -n "$header" | base64url_encode)
  57. payload_enc=$(echo -n "$payload" | base64url_encode)
  58. jwt_unsigned="${header_enc}.${payload_enc}"
  59.  
  60. # Write the private key to a temporary file
  61. temp_key=$(mktemp)
  62. # Replace literal "\n" sequences with actual newlines
  63. printf '%b' "$PRIVATE_KEY" > "$temp_key"
  64.  
  65. # Sign the JWT using the RS256 algorithm
  66. signature=$(echo -n "$jwt_unsigned" | openssl dgst -sha256 -sign "$temp_key" | base64url_encode)
  67. rm "$temp_key"
  68.  
  69. # Construct the final JWT
  70. JWT="${jwt_unsigned}.${signature}"
  71. echo "Generated JWT."
  72.  
  73. # Request an access token using the JWT
  74. response=$(curl -s -X POST https://oauth2.googleapis.com/token \
  75.   -H "Content-Type: application/x-www-form-urlencoded" \
  76.   -d "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=${JWT}")
  77.  
  78. access_token=$(echo "$response" | jq -r '.access_token')
  79. if [ "$access_token" == "null" ] || [ -z "$access_token" ]; then
  80.     echo "Error fetching access token: $response"
  81.     exit 1
  82. fi
  83. echo "Obtained access token."
  84.  
  85. # Construct the FCM message payload
  86. fcm_url="https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/messages:send"
  87. message_payload=$(jq -n \
  88.     --arg deviceToken "$DEVICE_TOKEN" \
  89.     --arg text "$TEXT_TO_SHOW" \
  90.     '{
  91.        validate_only: false,
  92.        message: {
  93.            token: $deviceToken,
  94.            android: { priority: "high" },
  95.            data: {
  96.                task: "Show Toast",
  97.                "%text": $text
  98.            }
  99.        }
  100.    }')
  101.  
  102. # Send the FCM message
  103. fcm_response=$(curl -s -X POST "$fcm_url" \
  104.   -H "Content-Type: application/json" \
  105.   -H "Authorization: Bearer ${access_token}" \
  106.   -d "$message_payload")
  107.  
  108. echo "FCM Response:"
  109. echo "$fcm_response"
  110.  
Comments
  • FuTChY
    106 days
    # text 0.19 KB | 0 0
    1. This script is just a conversion of this page: https://tasker.joaoapps.com/tests/remote%20task%20execution%20example.html
    2. That was made by João Dias the owner of Tasker so all credits go to him.
Add Comment
Please, Sign In to add comment