Skip to content

Instantly share code, notes, and snippets.

@tolpp
Last active April 4, 2024 16:33
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tolpp/45801406e13ed7c88fb768352bb39f9b to your computer and use it in GitHub Desktop.
Save tolpp/45801406e13ed7c88fb768352bb39f9b to your computer and use it in GitHub Desktop.
This gist explains how to send push notification locally using ADB without need network connection.

Requirements:

  • Device should be a rooted (simulator’s are rooted by default)

  • adbd should be started as root. (Rub command: adb root )

Now, send local push message using command:

adb shell am broadcast \
  -n com.your.app/com.google.firebase.iid.FirebaseInstanceIdReceiver \
  -a "com.google.android.c2dm.intent.RECEIVE" \
  --es "extra1" "65" \
  --es "guid" "1f400184-9215-479c-b19a-a9cd9a1d9dc9" \
  --es "extra3" "VALUE" \
  --es "extra4" "'Long string with spaces'"

What’s happening?

  • Simply, using adb we are broadcasting message with parameters.

  • adb shell am broadcast command that broadcasts.

  • -n parameter for define component (see IntentSpec@ADB Documentation)

  • We are broadcasting for com.google.firebase.iid.FirebaseInstanceIdReceiver, because we are using Firebase Messaging Service

  • We are sending action -a com.google.android.c2dm.intent.RECEIVE because FirebaseInstanceIdReceiver defines this in AndroidManifest.xml file.

    • Also, this is why we need to be root. This action requires Signature level permission.

  • We are adding extras with --es parameter.

Now, we can get data in onMessageReceived under the FirebaseMessagingService. When user taps notification remoteMessage.getData().get("extra1") should be return "65".

@darek607
Copy link

Thanks, it's helpful!

@zsperske
Copy link

Thanks for this gist! Really helpful. I'm curious, when I use a URL as one of the extra parameters like so:

--es url "https://www.mywebsite.com/u/webapp?param1=value1&param2=value2"

When ingested, the string is cut off as soon as the first & appears. Do urls have to be encoded?

@Mamadou888
Copy link

Thanks for the gist. For others who try this and it doesn't work, it might be because you do not have the below in your app's AndroidManifest. Feel free to add (but remove it before committing) in order to test. This is the other end of what the broadcast is plugging into.

<receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
    android:exported="true"
    android:permission="@null"
    tools:replace="android:permission">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <category android:name="com.gusto.money.presto" />
    </intent-filter>
</receiver>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment