【问题标题】:GCM messages are not received unless SEND permission is removed除非删除 SEND 权限,否则不会收到 GCM 消息
【发布时间】:2014-01-16 13:56:49
【问题描述】:

为了正确实施 GCM,应按照the official docs 为接收者指定com.google.android.c2dm.SEND 权限:

接收者应该需要 com.google.android.c2dm.SEND 权限,这样只有 GCM 框架才能向它发送消息。

但是,当我添加该权限时,收到消息时会收到此错误。

W/GTalkService(25224):[DataMsgMgr] 广播意图回调:result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.XXX.XXX](有附加功能)}

随后出现此错误:

W/ActivityManager(283): Permission Denial: 从 com.google 广播 Intent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.XXX.XXX] flg=0x10 (has extras) }由于接收方 com.XXX.XXX/com.XXX.XXX.GcmBroadcastReceiver,.android.syncadapters.contacts 需要 com.google.android.c2dm.SEND

如果我只删除该权限而不更改任何其他内容,则接收方可以正常工作并且我可以处理消息。

这是 AndroidManifest.xml 中的接收器定义

<receiver
        android:name="com.XXX.XXXX.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.XXX.XXX" />
        </intent-filter>
    </receiver>

我在测试期间使用调试证书,以防万一。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    试试com.google.android.c2dm.permission.SEND而不是com.google.android.c2dm.SEND,如:

    <receiver
      android:name="GCMBroadcastReceiverCompat"
      android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
        <category android:name="com.commonsware.android.gcm.client"/>
      </intent-filter>
    </receiver>
    

    (来自this sample app

    【讨论】:

    • 谢谢。那解决了它。官方文档中的示例清单实际上是正确的。前面的文字解释虽然使用了不正确的 com.google.android.c2dm.permission.SEND。
    【解决方案2】:

    试试这个:

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    
    <receiver
            android:name="com.robustastudio.mateegy.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
    
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    
                <category android:name="com.robustastudio.mateegy" />
            </intent-filter>
        </receiver>
    

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2014-10-19
      相关资源
      最近更新 更多