用于GCM的WakefulBroadcastReceiver未触发

时间:2015-08-08 08:30:47

标签: android broadcastreceiver google-cloud-messaging

好的,我已经搜索了这个主题并实施了所有可能的事情,仍然没有成功。 这是接收器类

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public final class MyGcmBroadcastReceiver extends  WakefulBroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

            Log.d("output something","Output");
            ComponentName comp = new ComponentName(context.getPackageName(),
            MyGcmIntentService.class.getName());
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);

}
}

这是清单的内容 注意:我使用的是来自不同库的不同包,其中包含接收者和intentservice

 <receiver
        android:exported="false"
        android:name="lib.package.name.MyGcmBroadcastReceiver"
        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" />
        </intent-filter>
         <category android:name="my.demo.app" />
    </receiver>
    <service  android:exported="false" android:name="lib.package.name.MyGcmIntentService" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <permission android:name="my.demo.app.permission.C2D_MESSAGE"  android:protectionLevel="signature" />
  <uses-permission android:name="my.demo.app.permission.C2D_MESSAGE" />

服务器正在投掷 &#34; multicast_id&#34;:8642316007520492123,&#34;成功&#34;:1,&#34;失效&#34;:0,&#34; canonical_ids&#34;:0,&#34;结果&# 34;:

我的GCM意向服务

public final class MyGcmIntentServiceextends GcmListenerService{
public static final int NOTIFICATION_ID = 1;   
NotificationCompat.Builder builder;

public MyGcmIntentService() {

    Log.d("Test GCMMMMMMMMM","GCM  SDS");
}

String app_tag="MyGcmIntentService->MyGcmIntentService";

@Override
public void onMessageReceived(String from, Bundle data) {
      String message = data.getString("message");
        Log.d(app_tag, "From: " + from);
        Log.d(app_tag, "Message: " + message);

        /**
         * Production applications would usually process the message here.
         * Eg: - Syncing with server.
         *     - Store message in local database.
         *     - Update UI.
         */

        /**
         * In some cases it may be useful to show a notification indicating  to the user
         * that a message was received.
         */
        sendNotification("web-demo",message);
}

我在logcat中看不到任何输出。 请帮帮我吧!

1 个答案:

答案 0 :(得分:0)

您还可以发布MyGcmIntentService吗?

根据 Google developers 实施GCM,Google建议迁移到GCMReceiver和GcmListenerService。此外,

  1. 在应用清单中,将您的GcmBroadcastReceiver替换为"com.google.android.gms.gcm.GcmReceiver",并将当前扩展IntentService的服务声明替换为新的GcmListenerService。
  2. 从客户端代码中删除BroadcastReceiver实现。
  3. 重构当前的IntentService服务实现以使用GcmListenerService 另请查看此 tutorial