【问题标题】:How to send FCM (firebase cloud messaging) push notification from ADB to device如何从 ADB 向设备发送 FCM(firebase 云消息传递)推送通知
【发布时间】:2017-03-10 10:56:43
【问题描述】:

我们正在使用 Firebase 云消息传递将推送通知发送到 Android 应用程序。

目前要测试推送通知,我们需要将消息发送到 FCM 服务器并等待消息到达设备。大多数情况下,设备需要很长时间才能从 FCM 服务器获取通知。

我可以在下面看到一些链接,这些链接解释了使用 adb 广播命令向设备发送推送通知(此示例解释了使用 GCM 框架发送消息,但我们使用 FCM) Is it possible to simulate a GCM receive from the adb shell / am command line? I'm getting an error

是否有任何类似的方法可以使用 adb 向具有 FCM 的设备发送推送通知?

【问题讨论】:

  • 你找到解决办法了吗?
  • 不是真的!但是,如果我没有立即收到消息,我只需打开/关闭飞行计划模式,以便我立即通过云收到消息。

标签: android firebase google-cloud-messaging firebase-cloud-messaging adb


【解决方案1】:

可以通过 adb 发送 FCM 负载。

虽然com.google.android.c2dm.permission.SEND 权限确实存在问题,但有一种解决方法。

gradle 将 FirebaseInstanceIdReceiver 添加到 merged 清单中。解决方法是将您的自己的副本添加到清单并覆盖权限 使用tools:replace="android:permission"android:permission="@null"

<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>

然后发出

adb shell "am broadcast -n your.package.name/com.google.firebase.iid.FirebaseInstanceIdReceiver -c your.package.name -a com.google.android.c2dm.intent.RECEIVE ... 通过终端

(PS - 我强烈建议仅在调试版本中进行,通过 gradle 的清单占位符或在您的调试/开发版本中使用单独的 AndroidManifest.xml)

【讨论】:

  • 如果有人在运行aapt dump badging (ERROR getting 'android:permission' attribute for receiver 'com.google.firebase.iid.FirebaseInstanceIdReceiver': attribute is not a string value) 时遇到此设置的问题。我能够通过删除行 android:permission="@null" 并将 tools:replace="android:permission"tools:ignore="ExportedReceiver" 放在它们的位置来解决它。
【解决方案2】:

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

在 AS 终端上运行这些命令:

  • adb root -> 为了获得com.google.android.c2dm.intent.RECEIVE的权限

  • 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"```
    

其中--es 字段与data 节点内的字段相对应:

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

【讨论】:

  • 我的手机不允许启用 root “adbd 不能在生产版本中以 root 身份运行”所以我尝试按照其他堆栈溢出答案以adb shell "su &lt;your suggestion&gt;" 运行命令,甚至尝试了旧的使用方法使用 FirebaseInstanceIdReceiver 的广播接收器命令,均无济于事...您还有其他提示吗?
  • @bamboo10 我只能考虑将它与 FCM 用户令牌一起使用:-console.firebase.google.com > 云消息传递 > 新通知 - 手动构建请求(您可以使用 Postman、Curl、..) : - URL:fcm.googleapis.com/fcm/send - 方法:POST - 标题参数:Content-Type: application/jsonAuthorization: key={server key} - 正文:{"data":{"title":"Title","body":"Body"},"to ":"{用户令牌}"}
【解决方案3】:

无法从 adb 命令发送推送通知。因此,您的进程需要以下权限才能通过 adb 发送广播。 但是google不允许设置com.google.android.c2dm.permission.SEND权限。

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"

你会得到以下异常

操作eration not allowed: java.lang.SecurityException: Package com.example.hunted has not requested permission com.google.android.c2dm.permission.SEND

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

./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

【讨论】:

    【解决方案4】:

    这在模拟器上对我有用: 只需将其粘贴到您的 Android Studio 终端中即可:

      adb shell am broadcast -a com.google.android.c2dm.intent.RECEIVE -n MyCOM.MyCOMPANY.MyAPP/com.google.firebase.iid.FirebaseInstanceIdReceiver --es "title" "Congradulation", --es "message" "this is your message", --es "isThisWorking" "yes"
    

    MyCOM.MyCOMPANY.MyAPP 位替换为您的应用程序包名称

    【讨论】:

      猜你喜欢
      • 2016-09-21
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多