设备没有收到新的谷歌云消息传递消息(NEW GCM) [英] Device Doesn't Receive message from NEW Google Cloud Messaging (NEW GCM)

查看:227
本文介绍了设备没有收到新的谷歌云消息传递消息(NEW GCM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我 我一直在上网这个话题,仍堆放在这个新的GCM

please help me i've been surfing about this topic , and still stack in this new GCM

我根据这个链接使用GCM推送最后一个项目

my last project using GCM Push according this link

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

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

但现在,最后的GCM(C2DM)是德precated,所以我们不使用任何GCMRegistrar了

but now, the last GCM (C2DM) is deprecated, so we don't use any GCMRegistrar anymore

下面,就来看看这个 ħ**号码://developer.android.com/google/gcm/c2dm.html 有一个说法的Andr​​oid云到设备消息(C2DM)是pcated德$ P $的C2DM服务将继续维持在短期内,但是 C2DM将不接受新用户,这将不授予新的配额

here, take a look at this h**p://developer.android.com/google/gcm/c2dm.html there is a statement "Android Cloud to Device Messaging (C2DM) is deprecated. The C2DM service will continue to be maintained in the short term, but C2DM will accept no new users, and it will grant no new quotas"

我用这个链接,学习的概念

i use this link for learning the concept

和的例子,我使用: 的https://$c$c.google.com/p/ GCM /来源/浏览/#混帐

and for the example i use : https://code.google.com/p/gcm/source/browse/#git

我想,我也跟着要求每步

i think, i have followed every steps required

我引用我的项目添加到Google Play服务的lib

i have referenced my project to google play service lib

我也改变了我的发件人ID为我的谷歌的项目数

i also have changed my sender id to my google project number

我也有收到REGID从GCM

i also have received RegID from GCM

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

but the problem is , whenever i click the send button

它从来没有告诉我任何事情。

it never show me anything

这是谷歌文档的例子,它说,它应该回显到我的设备,但它从来不

from google documentation example, it says that it should ECHO back to my device, but it never does

我检查的文件 这个概念说gcmbroadcastreceiver会收到任何GCM信息推送到我的设备,

i checked in documentation the concept says that gcmbroadcastreceiver will receive any GCM Message pushed to my device ,

所以我试图在广播里面登录了的onReceive 但它从来没有显示任何东西。

so i tried to log inside the "OnReceive" in that broadcaster, but it never show anything

我也尝试使用这个文件,以实现HTTP GCM服务器 http://developer.android.com/google/gcm/http.html

i also tried to implement HTTP GCM Server using this documentation http://developer.android.com/google/gcm/http.html

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

i get the response like this {"multicast_id":6256370624066466203,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1378114688323559%eab45603f9fd7ecd"}]}

但我的日志从来没有显示任何

but my log never shows anything

这是我的BroadcastReceiver

here is my broadcastreceiver

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);
    }
}

和这里是我的intentservice

and here is my intentservice

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());
    }
}

和这里是我的清单

<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>

我使用2.2 Froyo的

i'm using 2.2 FROYO

和我已经更新了我的谷歌播放服务,以最新的一个

and i've updated my google play services to the newest one

这样的家伙,如果有人可以请帮助我,非常感谢:)

so guys, if anyone could please help me , thanks a lot :)

推荐答案

试试这个方法

更新:新的GCM API实现

UPDATE : New GCM API implementation

1)创建类GcmIntentService.java

1) create class GcmIntentService.java

public class GcmIntentService extends IntentService {

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

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

}

2)创建类GcmBroadcastReceiver.java

2) create class 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);
    }

}

3)Mainfest.xml

3) Mainfest.xml

pemissions的GCM

pemissions for 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" />

----------------------旧的API ----------------------- -------------------------

----------------------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" />

创建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();
    }
}

这篇关于设备没有收到新的谷歌云消息传递消息(NEW GCM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆