Skip to content

Instantly share code, notes, and snippets.

@mediavrog
Created March 6, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediavrog/9bfbf7c78b09259bbf3907ef7daf8ceb to your computer and use it in GitHub Desktop.
Save mediavrog/9bfbf7c78b09259bbf3907ef7daf8ceb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Simulate push locally. Configure the proper `deviceToken`
# to receive a push notification locally and instantaneously in debug/beta apps.
#
# - must have "adb" (Android Device Bridge) installed and executable
# - for script usage see usage() below or run the script without any parameters
# - `com.google.firebase.iid.FirebaseInstanceIdReceiver` receiver must be `exported=true` in `debug/AndroidManifest`
#<application>
# <!-- Expose this receiver to simulate push notifications via /scripts/push/trigger.sh -->
# <receiver
# android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
# android:exported="true"
# tools:remove="android:permission">
# </receiver>
#</application>
# check prerequisites
if ! [[ -x "$(command -v adb)" ]]; then
echo "Please install 'adb' and add it to your \$PATH before running this script."
exit 1
fi
if ! [[ -x "$(command -v jq)" ]]; then
echo "Please install 'jq' and add it to your \$PATH before running this script."
exit 1
fi
# setup command
function usage() {
echo "Usage: $0 [-t <deviceToken>] [-s <adbDeviceId>] <path_to_push_json_payload>"
}
# override the device token in the push payload
DEVICE_TOKEN=
# use a specific adb device by passing the device id (adb devices)
ADB_DEVICE_ID=
while [[ "$1" == -* ]]; do
case "$1" in
-t)
shift
DEVICE_TOKEN="$1"
;;
-s)
shift
ADB_DEVICE_ID="-s $1"
;;
esac
shift
done
readonly PUSH_CONFIG_FILE=${1?$( usage )}
# Default params to construct the intent
PARAMS=(-n "DEBUG_APPLICATION_ID/com.google.firebase.iid.FirebaseInstanceIdReceiver")
PARAMS+=(-a "com.google.android.c2dm.intent.RECEIVE")
# Read JSON file with push contents and add as string intent extras
while IFS="=" read -r key value
do
if ! [[ -z "$DEVICE_TOKEN" ]] && [[ "$key" == "deviceToken" ]]
then
PARAMS+=("--es \"$key\" \"$DEVICE_TOKEN\"")
else
PARAMS+=("--es \"$key\" \"$value\"")
fi
done < <(jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" $PUSH_CONFIG_FILE)
#echo "adb $ADB_DEVICE_ID shell am broadcast "${PARAMS[@]}""
adb $ADB_DEVICE_ID shell am broadcast "${PARAMS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment