4

我尝试从 gcm.jar 更新以从 google-play-services.jar 获取 GCM。我正在使用与此处所示相同的代码。

我使用与更改客户端实现之前相同的服务器(node-gcm)。

我得到了registrationId,但是当我尝试发送通知时,没有调用GcmBroadcastReceiver.onReceive。(我在清单文件中有它)

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;

/**
 * Created with IntelliJ IDEA.
 * User: Eran
 * Date: 11/11/13
 * Time: 00:43
 * To change this template use File | Settings | File Templates.
 */
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("GcmBroadcastReceiver", intent.getDataString());
        // 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);
    }
}

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gamerlabs.high5poker"
    android:versionCode="3"
    android:versionName="3.0">
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <supports-screens
            android:anyDensity="true"
            android:largeScreens="true"
            android:normalScreens="true"
            android:smallScreens="true"

            >
    </supports-screens>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="com.android.vending.BILLING" />

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

    <application
        android:name="com.gameralabs.classes.HFApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:hardwareAccelerated="true"
        android:largeHeap="true"
        android:debuggable="true"
            >

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />

        <activity android:name="com.facebook.LoginActivity"/>
        <activity
            android:name="com.gameralabs.high5poker.HFSplashActivity"
            android:screenOrientation="landscape"
            android:noHistory="true"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <activity
            android:name="com.gameralabs.high5poker.HFLoginActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
             >
        </activity>
        <activity
            android:name="com.gameralabs.high5poker.HFLobbyActivity"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
             >
        </activity>
        <activity
                android:name="com.gameralabs.high5poker.HFGameActivity"
                android:noHistory="true"
                android:screenOrientation="landscape"
                android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                >
        </activity>
        <activity android:name="com.gameralabs.high5poker.HFActivity"/>

        <receiver
                android:name="com.gameralabs.high5poker.GcmBroadcastReceiver"
                android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.gameralabs.high5poker" />
            </intent-filter>
        </receiver>
        <service android:name="com.gameralabs.high5poker.GcmIntentService" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    </application>

</manifest>

有什么帮助吗?

4

2 回答 2

2

您忘记更改权限中的包名称。

<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.mintmark.TestGcm.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="com.mintmark.TestGcm.permission.C2D_MESSAGE" />
于 2013-11-11T15:28:07.733 回答
0

请注意,新的 GCM 实现已更新为声明我们应该WakefulBroadcastReceiver从代码中删除 。

根据新样本:

在应用清单中,将您的 GcmBroadcastReceiver 替换为“com.google.android.gms.gcm.GcmReceiver”,并将当前扩展 IntentService 的服务声明替换为新的 GcmListenerService

从您的客户端代码中删除 BroadcastReceiver 实现

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
                SharedPreferences sharedPreferences =
                        PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences
                        .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
                if (sentToken) {
                    mInformationTextView.setText(getString(R.string.gcm_send_message));
                } else {
                    mInformationTextView.setText(getString(R.string.token_error_message));
                }
            }
        };

   @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    }

    @Override
    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
        super.onPause();
    }
于 2015-06-15T19:27:50.657 回答