我有一个老问题:我无法使用Android应用程序接收GCM消息。根据服务器日志,它们已成功传送到GCM服务器并进一步发送,但我的客户端应用程序似乎没有收到它们。
这是我清单中与GCM相关的片段:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission
android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />
<category android:name="my.package" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
这是我的服务类GcmIntentService:
public class GcmIntentService extends GcmListenerService {
private String TAG = "myReceiver";
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d(TAG, "message received");//this log is never shown
if (!data.isEmpty()) {
Log.d(TAG, "interesting message receiver");
}
}
}
这是我的项目build.gradle依赖项:
classpath 'com.google.gms:google-services:1.3.0-beta1'
我想我忘了添加一些东西,这就是它根本不起作用的原因。我试过WLAN和SIM卡上网。所以问题不在互联网防火墙中。发件人API和GCM令牌似乎是正确的(它们是从开发者帐户的相应设置中复制和粘贴的)。
提前感谢您的帮助。
编辑:请参阅下面的答案
答案 0 :(得分:1)
在清单中执行以下更改
替换以下代码
<service android:name=".GcmIntentService" />
带
<service
android:name="packagename.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
并在
<uses-permission android:name="your.package.name.permission.C2D_MESSAGE" />
写下你的包名称代替的任何内容 &#34; your.package.name&#34;
答案 1 :(得分:0)
有些事情是错的/可能是错的。您需要主活动的gcm注册码中的发件人ID。您有C2D_MESSAGE
权限的3倍,这是第一次出错(put.your.package.name
)。第二次它对于生产阶段是正确的,但不适用于开发,并且第三次获得正确的开发权限。
你应该只有一次。
确实像@Android india建议在清单中更改您的服务声明。
答案 2 :(得分:0)
请看这个
你的接收者声明有一些错误
<receiver
android:name="yourpackage.receiver.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="yourpackage" />
</intent-filter>
</receiver>
答案 3 :(得分:0)
Android Manifest片段有效:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission
android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="my.package" />
</intent-filter>
</receiver>
<service
android:name="my.package.GcmListener"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>