Is it possible to simulate a GCM receive from the adb shell / am command line? I'm getting an error

后端 未结 4 1377
难免孤独
难免孤独 2020-12-30 00:01

I\'m trying to simulate as if the device is receiving a GCM push message by using adb and the command line. I\'ve tried this command to broadcast a GCM intent:



        
相关标签:
4条回答
  • 2020-12-30 00:34

    Better than temporarily editing AndroidManifest.xml to remove the permission check, and remembering to restore it later, you can automatically disable the permission check in debug builds.

    To do this, set the attribute via a Manifest Merger placeholder:

    <receiver
      android:name="com.google.android.gcm.GCMBroadcastReceiver"
      android:permission="${gcmPermissionRequired}">
    

    then set the placeholder in build.gradle:

    buildTypes {
        debug {
            ...
            manifestPlaceholders = [gcmPermissionRequired: ""] // "" => let the GCM BroadcastReceiver accept Intents from 'adb shell am broadcast'
        }
        release {
            ...
            manifestPlaceholders = [gcmPermissionRequired: "com.google.android.c2dm.permission.SEND"]
        }
    }
    

    (Note: Previously I used debug & release string resources. It turns out that the Play store rejects the app if it defines an intent filter permission using a string resource.)

    0 讨论(0)
  • 2020-12-30 00:46

    I suggest using command-line curl, as sending GCM pushes is as easy as calling some REST API. See sample shell script below:

    #!/bin/bash
    
    REGISTRATION_ID=YOUR_GCM_REGISTRATION_ID
    
    SERVER_KEY=YOUR_SERVER_KEY_FROM_GOOGLE_API_CONSOLE
    
    curl --header "Authorization: key=$SERVER_KEY" --header  Content-Type:"application/json"  https://android.googleapis.com/gcm/send  -d  "{ \"data\" : {\"foo\": \"bar\"}, \"registration_ids\":[\"$REGISTRATION_ID\"]  }"
    
    0 讨论(0)
  • 2020-12-30 00:48

    You need to remove the permission property in your receiver's definition like this:

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <application>
    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.myapp" />
        </intent-filter>
    </receiver>
    </application>
    
    0 讨论(0)
  • 2020-12-30 01:00
    1. Install adb first.

    sudo apt-get install adb

    1. Remove the SEND permission from manifest.xml.

    android:permission="com.google.android.c2dm.permission.SEND" >

    1. Command to push notifications from shell

    adb shell am broadcast -c com.myapp -a com.google.android.c2dm.intent.RECEIVE -e data "SomeData"

    1. Change the package name (com.myapp) and "SomeData" as you want.
    0 讨论(0)
提交回复
热议问题