【问题标题】:GCM Does Not Receive Message Below Android 4GCM 在 Android 4 以下不接收消息
【发布时间】:2013-12-10 14:28:33
【问题描述】:

我的 Gcm 应用程序收到来自 Android 4 及更高版本的 GCM 的良好消息。但是下面我的应用程序没有收到消息。

这是 GcmBroadcastReceiver:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver 
{

    @Override
    public void onReceive(Context context, Intent intent) 
        {
            // TODO Auto-generated method stub

            ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
            Log.i("GCM BROADCAST", "Begin Broadcast");

            startWakefulService(context, intent.setComponent(comp));

            setResultCode(Activity.RESULT_OK);
        }
}

这是意图:

public class GCMIntentService extends IntentService 
{
    public static final String TAG = "GcmIntentService";
    public static final int NOTIFICATION_ID = 1;

    public GCMIntentService()
        {
            super(Constant.SENDER_ID);
        }

/* (non-Javadoc)
 * @see android.app.IntentService#onHandleIntent(android.content.Intent)
 */
@Override
protected void onHandleIntent(Intent intent)
    {
        // TODO Auto-generated method stub

        Bundle extras = intent.getExtras();

        // Prendo l'istanza di Google Cloud Messaging
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        // Prendo la stringa che mi indica il tipo di messaggio
        String messageType = gcm.getMessageType(intent);

        Log.i( TAG , "Messaggio ricevuto: " + extras.toString());

        if(!extras.isEmpty())
            {   
                // Azione nel caso il messaggio sia di errore
                if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
                    sendNotification("Send error: " + extras.toString());
                // Azione nel caso il messaggio sia riguardo l'eliminazione sul server
                else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
                    sendNotification("Deleted messages on server: " + extras.toString());
                // Azione nel caso sia un messaggio rigardante la nostra applicazione
                else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType))
                    {
                        sendNotification(extras.getString("message"));
                    }
            }

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

这是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcmclient"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

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

<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

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

    <receiver
        android:name="com.example.gcmclient.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.gcmclient.broadcastreceivers" />
        </intent-filter>
    </receiver>
    <service android:name="com.example.gcmclient.GCMIntentService" />
    <service android:name="com.example.gcmclient.RegistrationService"></service>
    <receiver 
       android:name="com.example.gcmclient.broadcastreceivers.BootHandler"
       android:enabled="true"
       android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

</application>

我不明白为什么它不能在 Android 4 以下运行?提前致谢。

【问题讨论】:

    标签: android android-intent broadcastreceiver google-cloud-messaging google-play-services


    【解决方案1】:

    改变

    <permission android:name="com.example.gcm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
    

    <permission android:name="com.example.gcmclient.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.gcmclient.permission.C2D_MESSAGE" />
    

    <receiver
        android:name="com.example.gcmclient.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.gcmclient.broadcastreceivers" />
        </intent-filter>
    </receiver>
    

    <receiver
            android:name="com.example.gcmclient.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gcmclient" />
            </intent-filter>
        </receiver>
    

    【讨论】:

    • 非常感谢,现在可以使用了!只是出于好奇,问题是什么?
    • 不客气!您没有正确定义接收者的 GCM 权限和类别。它们都应该与您的应用程序的主包相匹配。出于某种原因,这只会在较旧的 Android 版本中引起问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多