gcm GoogleCloudMessaging新实施

时间:2013-11-11 14:04:43

标签: android push-notification google-cloud-messaging google-play-services

我尝试从gcm.jar更新以从google-play-services.jar获取GCM。 我使用的代码与显示的here相同。

我正在使用与更改客户端实现之前相同的服务器(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>

任何帮助?

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

请注意,新的GCM实施已更新,说明我们应从代码中删除WakefulBroadcastReceiver

根据新样本:

  

在应用清单中,将GcmBroadcastReceiver替换为   &#34; com.google.android.gms.gcm.GcmReceiver&#34;,并替换当前   将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();
    }