收到GCM消息但未调用服务功能

时间:2015-08-07 14:08:10

标签: android google-cloud-messaging

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");
    }
}

Catlog打印

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

这与发送的有效负载有关吗?

3 个答案:

答案 0 :(得分:1)

根据Google的官方声明https://developers.google.com/cloud-messaging/android/legacy-regid

GCM register()自2015年5月28日起不再使用。新的应用程序开发应使用Instance ID API来处理注册令牌的创建,轮换和更新。有关更多信息,请参阅在Android上注册客户端应用程序和实施GCM客户端。

如果您仍想使用旧方法,请删除 gcm.jar 忘记 GCMBaseIntentService ,并使用 WakefulBroadcastReceiver 作为 GcmBroadcastReceiver 并为 GcmIntentService

使用 IntentService
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);
    }
}

答案 1 :(得分:0)

将此权限添加到您的清单

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

并实现此类

public class GcmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.info(TAG, "Received GCM message");
}

答案 2 :(得分:0)

我认为你应该像这样添加

<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) {
        ...
     } 
.. 
}