收到 GCM 消息但未调用服务功能
GCM message received but service function is not invoked
onMessage 不是从 Intent-Service 触发的
我的清单内容
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.permission.C2D_MESSAGE" />
<receiver android:name="com.example.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="com.example" />
</intent-filter>
</receiver>
<service android:name="com.example.GcmIntentService"/>
以前可以用,现在不行了
GcmIntentService.java
public class GcmIntentService extends GCMBaseIntentService {
public GcmIntentService() {
super(SENDER_ID);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.info(TAG, "Received GCM message");
}
}
已打印目录
08-07 19:19:36.199: V/GCMBroadcastReceiver(6538): onReceive: com.google.android.c2dm.intent.RECEIVE
08-07 19:19:36.199: V/GCMBroadcastReceiver(6538): GCM IntentService class: com.example.GcmIntentService
08-07 19:19:36.199: V/GCMBaseIntentService(6538): Acquiring wakelock
08-07 19:19:36.239: V/GCMBaseIntentService(6538): Releasing wakelock
这与发送的负载有关吗?
将此权限添加到您的清单
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
并实施此 class
public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.info(TAG, "Received GCM message");
}
我认为你应该这样添加
<service android:name=".service.GcmService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
然后
public class GcmService extends GcmListenerService {
public GcmService() {
...
}
@Override
public void onMessageReceived(String from, Bundle data) {
...
}
..
}
根据 Google https://developers.google.com/cloud-messaging/android/legacy-regid 的官方声明
GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens. For more information, see Registering Client Apps and Implementing GCM Client on Android.
如果您仍想使用旧方法,请删除 gcm.jar 忘记 GCMBaseIntentService 并使用 WakefulBroadcastReceiver 用于您的 GcmBroadcastReceiver 并使用 IntentService 用于您的 GcmIntentService
GoogleCloudMessaging.getInstance(YourActivity.this).register(SENDER_ID)
在 AsyncTask 中,然后它会工作。
您可能还需要权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="[yourpackage].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[yourpackage].permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
GcmIntentService 代码
public class GcmIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
switch (messageType) {
case GoogleCloudMessaging.MESSAGE_TYPE_DELETED:
break;
case GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE:
break;
case GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR:
default:
break;
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
GcmBroadcastReceiver 代码
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
public final void onReceive(final Context context, final Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
onMessage 不是从 Intent-Service 触发的
我的清单内容
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.permission.C2D_MESSAGE" />
<receiver android:name="com.example.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="com.example" />
</intent-filter>
</receiver>
<service android:name="com.example.GcmIntentService"/>
以前可以用,现在不行了
GcmIntentService.java
public class GcmIntentService extends GCMBaseIntentService {
public GcmIntentService() {
super(SENDER_ID);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.info(TAG, "Received GCM message");
}
}
已打印目录
08-07 19:19:36.199: V/GCMBroadcastReceiver(6538): onReceive: com.google.android.c2dm.intent.RECEIVE
08-07 19:19:36.199: V/GCMBroadcastReceiver(6538): GCM IntentService class: com.example.GcmIntentService
08-07 19:19:36.199: V/GCMBaseIntentService(6538): Acquiring wakelock
08-07 19:19:36.239: V/GCMBaseIntentService(6538): Releasing wakelock
这与发送的负载有关吗?
将此权限添加到您的清单
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
并实施此 class
public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.info(TAG, "Received GCM message");
}
我认为你应该这样添加
<service android:name=".service.GcmService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
然后
public class GcmService extends GcmListenerService {
public GcmService() {
...
}
@Override
public void onMessageReceived(String from, Bundle data) {
...
}
..
}
根据 Google https://developers.google.com/cloud-messaging/android/legacy-regid 的官方声明
GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens. For more information, see Registering Client Apps and Implementing GCM Client on Android.
如果您仍想使用旧方法,请删除 gcm.jar 忘记 GCMBaseIntentService 并使用 WakefulBroadcastReceiver 用于您的 GcmBroadcastReceiver 并使用 IntentService 用于您的 GcmIntentService
GoogleCloudMessaging.getInstance(YourActivity.this).register(SENDER_ID)
在 AsyncTask 中,然后它会工作。
您可能还需要权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<permission android:name="[yourpackage].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[yourpackage].permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
GcmIntentService 代码
public class GcmIntentService extends IntentService {
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
switch (messageType) {
case GoogleCloudMessaging.MESSAGE_TYPE_DELETED:
break;
case GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE:
break;
case GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR:
default:
break;
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
GcmBroadcastReceiver 代码
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
public final void onReceive(final Context context, final Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}