Android 未调用GcmListenerService.onMessageReceived
我正在尝试从服务器向我的应用程序发送消息。 我有一个向所有用户发送消息的服务器url: 我的应用的令牌已在服务器中注册。。。但是我有一个问题,就是我的GcmListenerService实现中的onMessageReceived()方法没有被调用 我的舱单:Android 未调用GcmListenerService.onMessageReceived,android,service,google-cloud-messaging,Android,Service,Google Cloud Messaging,我正在尝试从服务器向我的应用程序发送消息。 我有一个向所有用户发送消息的服务器url: 我的应用的令牌已在服务器中注册。。。但是我有一个问题,就是我的GcmListenerService实现中的onMessageReceived()方法没有被调用 我的舱单: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.atheel.prayer" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="com.example.atheel.prayer.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.atheel.prayer" />
</intent-filter>
</receiver>
<service
android:name="com.example.atheel.prayer.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.atheel.prayer.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service android:exported="false" android:name="com.example.atheel.prayer.RegistrationIntentService"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EmptyStatus"
android:label="@string/title_activity_empty_status" >
</activity>
<activity
android:name=".UnEmpty"
android:label="@string/title_activity_un_empty" >
</activity>
</application>
</manifest>
任何人都可以想一想为什么它不起作用?我想您已经阅读了GCM支持手册。我也建议你们读这篇文章 与我的GCM代码相比,你漏掉了一行
<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/>
}
此组件调用以下服务
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super(App.getStr(R.string.gcm_sender_id)); // this integer is provided when registering the application
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(extras);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(extras);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// here you can do your work
}
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
}
PS:请注意,我的解决方案深受互联网上其他解决方案的启发,例如
谢谢。1) 丢失的线路应该在接收器或服务中?2) 您键入的是什么而不是签名?缺少的行在receiver中,“签名”不是占位符,应该是“签名”。检查这里PS:代码中唯一的占位符是your.package-这应该是您的基本包,应该是com.example.atheel.prairi发布的编辑。如果问题仍然存在,请通过我的网站martinvano.sk与我联系。“我相信我们能以某种方式修复它。@vanomart我想你仍然在使用旧的API。”
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission
android:name="your.package.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
public class GcmIntentService extends IntentService {
public GcmIntentService() {
super(App.getStr(R.string.gcm_sender_id)); // this integer is provided when registering the application
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification(extras);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification(extras);
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// here you can do your work
}
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}