广播接收器无法接收Google Cloud Messaging

时间:2014-10-13 18:02:21

标签: java android google-cloud-messaging

我已经学习了几天的Google Cloud Messaging API,现在,我的应用程序出错了。 首先,我通过php构建我的服务器,它返回关于GCM的成功消息,例如:

{"multicast_id":7148194663931263470,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1413222862155080%099070adf9fd7ecd"}]}

但是在我的应用程序中,我尝试了很多方法来确保我的应用程序将收到消息。但我失败了。我的申请没有收到任何东西。

有人帮助我。这是Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dangchienhsgs.giffus" >

    <permission
        android:name="com.dangchienhsgs.giffus.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.dangchienhsgs.giffus.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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=".HomeActivity"
            android:label="@string/title_activity_home" >
        </activity>

        <receiver
            android:name=".GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.dangchienhsgs.giffus" />
            </intent-filter>
        </receiver>

        <provider
            android:name=".provider.DataProvider"
            android:authorities="com.dangchienhsgs.giffus.provider"
            android:exported="false" >
        </provider>

        <service android:name=".GcmIntentService"/>

        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register" >
        </activity>

        <!--<service android:name=".account.AuthenticatorService" >
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>

            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service> -->

    </application>

</manifest>

这是GCMBroadcastReceiver.java

package com.dangchienhsgs.giffus;

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;
import android.widget.Toast;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
    private static final String TAG="GCMBroadcastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        // Sure that GCMIntentService will handle the intent
        Log.d(TAG, "is Received");
        Log.d(TAG, "Our message is received");
        ComponentName comp=new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }


}

以下是我的Github链接:&#34; https://github.com/dangchienhsgs/Giffus&#34;

1 个答案:

答案 0 :(得分:1)

我检查了您的代码以接收消息,似乎没问题。

您是否将注册设备实施为GCM:https://developer.android.com/google/gcm/client.html#sample-register