首页 文章

GCM - GCMBroadcastReceiver没有被调用

提问于
浏览
1

我创建了一个GCM Intent服务,设备已成功注册,服务器成功发送消息 . 但设备不会收到任何通知 .

在我看到的日志下面,

10-29 18:26:09.599: V/GCMBroadcastReceiver(30775): onReceive: com.google.android.c2dm.intent.RECEIVE
10-29 18:26:09.599: V/GCMBroadcastReceiver(30775): GCM IntentService class: com.eye.hor.GCMIntentService
10-29 18:26:09.599: V/GCMBaseIntentService(30775): Acquiring wakelock

下面是GCMBroadcastReceiver的代码,

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.i(context.getPackageName(), intent.getExtras().toString());
    Log.d("brdR", "askd");
    // Explicitly specify that GcmIntentService will handle the 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);
}

}

下面是GCMIntentService的代码,

public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GcmIntentService() {
    super("GcmIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that
         * GCM will be extended in the future with new message types, just
         * ignore any message types you're not interested in, or that you
         * don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            // sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            // sendNotification("Deleted messages on server: " +
            // extras.toString());
            // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // This loop represents the service doing some work.
            // for (int i = 0; i < 5; i++) {
            // Log.d("", "Working... " + (i + 1) + "/5 @ " +
            // SystemClock.elapsedRealtime());
            // try {
            // Thread.sleep(5000);
            // } catch (InterruptedException e) {
            // }
            // }
            Log.d("true", "Completed work @ " + SystemClock.elapsedRealtime());
            // Post notification of received message.
            sendNotification(extras.getString("msg"), extras.getString("title"), extras.getString("id"));

            Log.d("true", "Received: " + extras.toString());
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg, String title, String id) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    int notification_id = Integer.parseInt(id);

    // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new
    // Intent(this, CommonUtils.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContentTitle(title)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    // mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(notification_id, mBuilder.build());
}

}

这很明显,

<!-- 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" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="com.eye.hor.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.eye.hor.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

清单中的接收器代码,

<receiver
        android:name="com.google.android.gcm.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.eye.hor.service" />
        </intent-filter>
    </receiver>

    <service android:name="com.eye.hor.service.GCMIntentService" />

我的主要包名是com.eye.hor,但我在com.eye.hor.service包中放置了GCMIntentService和GCMBroadcastReceiver java文件 . 我已经把这两个类登录了但是没有到达 . 请帮忙

3 回答

  • 1

    看起来您已经知道包名有问题:

    以下内容在错误的包中查找您的服务类:

    ComponentName comp = 
        new ComponentName(context.getPackageName(), // this is the main package of
                                                    // your app - com.eye.hor
                          GcmIntentService.class.getName());
    

    将其更改为:

    ComponentName comp = 
        new ComponentName(GcmIntentService.class.getPackage().getName(), 
                          GcmIntentService.class.getName());
    

    您还应该将清单中的类别更改为主包:

    <category android:name="com.eye.hor.service" />
    

    <category android:name="com.eye.hor" />
    
  • 0
    <category android:name="com.eye.hor.service" />
    

    这不应该是您的Broadcastreceiver所在的软件包,这是谷歌开发者控制台中定义的软件包 .

    我愿意在你的谷歌开发者控制台中下注你定义的包有com.eye.hor,在这种情况下你必须将这一行更改为:

    <category android:name="com.eye.hor" />
    
  • 1
    • 您开发应用的设备是什么?在不同的设备上有异国情调的设置对后台工作的影响,在设备中检查这样的设置(“自动启动”,“推送通知”等,他们可以在一些设备上调用不同,如Xiamo,华为等) .

    • 您确定您的推送服务器使用了正确的“发件人ID”和“api密钥”参数,您可以在开发人员控制台中查看这些参数吗?

    • 检查Google Play服务是否安装正确 .

相关问题