提问者:小点点

如何从ADB向设备发送FCM(firebase cloud messaging)推送通知


我们正在使用firebase云消息将推送通知发送到android应用程序中。

目前,为了测试推送通知,我们需要将消息发送到FCM服务器,并等待消息到达设备。大部分时间设备需要很长时间才能从FCM服务器获得通知。

我可以看到下面的一些链接,这些链接解释了使用adb广播命令向设备发送推送通知(本示例解释了使用GCM框架发送消息,但我们使用的是FCM)是否可以从adb shell/am命令行模拟GCM接收?我收到一个错误

有没有类似的方法发送推送通知使用adb到设备有FCM?


共3个答案

匿名用户

它在模拟器上为我工作(您既不需要服务器密钥也不需要客户端令牌)。

在AS终端上运行以下命令:

> -&>;为了获得权限

adb shell am broadcast \
  -n <YOUR.APP.PACKAGE>/com.google.firebase.iid.FirebaseInstanceIdReceiver \
  -a "com.google.android.c2dm.intent.RECEIVE" \
  --es "title" "Title" \
  --es "body" "Body"```

其中字段与节点内的字段对应:

{
  "data": {
    "title": "Title",
    "body": "Body"
  },
  "to" : ""
}

匿名用户

可以通过ADB发送FCM有效载荷。

虽然权限确实是个问题,但有一个解决方法。

gradle将添加到合并的清单中。解决方法是将您自己的副本添加到清单中,并使用覆盖权限

<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="your.package.name" />
        </intent-filter>
</receiver>

然后发出

通过终端

(PS-I强烈建议仅在调试构建中通过Gradle's manifest placeholder或调试/开发构建中单独的androidmanifest.xml执行此操作)

匿名用户

null

If you run below command and try to grant send permission to your package.
./adb shell pm grant com.example.hunted "com.google.android.c2dm.permission.SEND"

您将得到以下异常

不允许Opcodeeration:java.lang.SecurityException:包com.example.Hunted没有请求权限com.google.android.c2dm.permission.send/code>

即使您将此权限添加到包中

./adb shell pm grant  com.example.hunted com.google.android.c2dm.permission.SEND
Operation not allowed: java.lang.SecurityException: Permission com.google.android.c2dm.permission.SEND is not a changeable permission type.

最后,当您使用ADB发送广播时。您将得到以下异常。

BroadcastQueue: Permission Denial: broadcasting Intent { flg=0x400010 cmp=com.example.hunted/com.google.firebase.iid.FirebaseInstanceIdReceiver (has extras) } from null (pid=32279, uid=2000) requires com.google.android.c2dm.permission.SEND due to receiver com.example.hunted/com.google.firebase.iid.FirebaseInstanceIdReceiver