设备无法接收来自新Google云消息传递的消息(NEW GCM)

Yog*_*nto 3 google-app-engine android android-intent

请帮助我,我一直在浏览这个主题,并仍然堆叠在这个新的GCM

我根据此链接使用GCM Push的上一个项目

HT**://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

但现在,最后一个GCM(C2DM)已被弃用,因此我们不再使用任何GCMRegistrar

在这里,看看这个h**p://developer.android.com/google/gcm/c2dm.html有一条声明"不推荐使用Android Cloud to Device Messaging(C2DM).C2DM服务将继续在短期内维持,但C2DM将不接受新用户,并且不会授予新的配额 "

我用这个链接来学习这个概念

以及我使用的示例:https: //code.google.com/p/gcm/source/browse/#git

我想,我已经遵循了所需的每一步

我已将我的项目引用到谷歌播放服务库

我也将发件人ID更改为我的Google项目编号

我也收到了GCM的RegID

但问题是,每当我点击发送按钮

它从来没有给我任何东西

从谷歌文档示例中,它说它应该回到我的设备ECHO,但它永远不会

我检查了文档中的概念说gcmbroadcastreceiver会收到任何推送到我设备的GCM消息,

所以我试图登录该广播公司的"OnReceive",但它从未显示任何内容

我还尝试使用此文档http://developer.android.com/google/gcm/http.html实现HTTP GCM Server

我收到像这样的响应{"multicast_id":6256370624066466203,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1378114688323559%eab45603f9fd7ecd"}] }

但我的日志从未显示任何内容

这是我的广播接收器

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.v("HAHA BANGET","masuk ke broadcastReceiver");

        // 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的意图服务

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

    public GcmIntentService() {
        super("7134XXXXX");
    }
    public static final String TAG = "HAHA";

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.v(TAG,"MASUK INTENT NIH");

        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.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "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) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

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

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.common_signin_btn_icon_dark)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的清单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name="com.coba.pushgcmbaru.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.coba.pushgcmbaru" />
        </intent-filter>
    </receiver>
    <service android:name="com.coba.pushgcmbaru.GcmIntentService" />

    <activity
        android:name="com.coba.pushgcmbaru.PushBaruLagi"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

我正在使用2.2 FROYO

我已将我的谷歌播放服务更新到最新版本

所以伙计们,如果有人能帮助我,非常感谢:)

Bir*_*dia 8

试试这种方式

更新:新的GCM API实现

1)创建类GcmIntentService.java

public class GcmIntentService extends IntentService {

    public GcmIntentService() {
        super("your project id here");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // handle message here
    }

}
Run Code Online (Sandbox Code Playgroud)

2)创建类GcmBroadcastReceiver.java

public class GcmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        context.startService((intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }

}
Run Code Online (Sandbox Code Playgroud)

3)Mainfest.xml

GCM的提交

<permission
        android:name="your.package.name.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

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

<receiver
            android:name="your.package.name.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />

                <category android:name="your.package.name" />
            </intent-filter>
        </receiver>

        <service
            android:name="your.package.name.GcmIntentService"
            android:enabled="true" />
Run Code Online (Sandbox Code Playgroud)

---------------------- OLD API -------------------------- ----------------------

public GcmIntentService() {
        super("72544799xxx"); //Pass GCM project id here
    }


<receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />

                <category android:name="com.coba.pushgcmbaru" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.coba.pushgcmbaru.GCMIntentService"
            android:enabled="true" />
Run Code Online (Sandbox Code Playgroud)

创建GCMReceiver类并替换为您的GcmBroadcastReceiver

import com.google.android.gcm.GCMBroadcastReceiver;

/**
 * This Class Contains All Method Related To GCMReceiver.
 * 
 * @author 
 * 
 */
public class GCMReceiver extends GCMBroadcastReceiver {

    @Override
    protected String getGCMIntentServiceClassName(Context context) {
        return GCMIntentService.class.getName();
    }
}
Run Code Online (Sandbox Code Playgroud)