GCM的Andr​​oid 4.0.4不能得到消息 [英] GCM Android 4.0.4 cant get message

查看:361
本文介绍了GCM的Andr​​oid 4.0.4不能得到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着从 developers.google.com 中的例子,创建简单服务从GCM检索令牌和消息。

I followed the example from developers.google.com and create simple services for retrieve Token and messages from GCM.

令牌类接收

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";

    private static final String[] TOPICS = {"global"};

    private String projectNumber = "gcm-test-xxxxx";

    public RegistrationIntentService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        try {

            InstanceID instanceID = InstanceID.getInstance(this);
            String token = instanceID.getToken(projectNumber,
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

            sendRegistrationToServer(token);
            subscribeTopics(token);
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, true).apply();


        } catch (IOException e) {
            Log.d(TAG, "Failed to complete token refresh", e);
            sharedPreferences.edit().putBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false).apply();
        }
        Intent registrationComplete = new Intent(QuickstartPreferences.REGISTRATION_COMPLETE);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void subscribeTopics(String token) {

    }

    private void sendRegistrationToServer(String token) throws IOException {
        GcmPubSub pubSub = GcmPubSub.getInstance(this);
        for (String topic : TOPICS) {
            pubSub.subscribe(token, "/topics/" + topic, null);
        }
    }

}

本类工作得很好,我可以检索令牌,做我想做的事情。
我开始这个类作为一个服务从 MainActivity

This class works fine, I can retrieve token and do what i want with it. I am starting this class as a service from MainActivity

    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, RegistrationIntentService.class));
    }
}

我也有类从GCM检索消息。这个类不工作的。

Also i have class for retrieving messages from GCM. This class don't work at all.

public class MyGcmListenerService extends GcmListenerService {
    String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        String message = data.getString("message");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        }    
}

所有这些东西是注册在清单。

All this stuff was registered in manifest.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="gcm_test.app">


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


    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
        <activity
                android:name=".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.google.android.gms.gcm.GcmReceiver"
                android:exported="true"
                android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="gcm_test.app" />
            </intent-filter>
        </receiver>

         <service
                 android:name=".gcm.MyGcmListenerService"
                 android:exported="false">
             <intent-filter>
                 <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
             </intent-filter>
         </service>

        <service
                android:name=".gcm.RegistrationIntentService"
                android:exported="false">
        </service>

    </application>

</manifest>

我发送消息与邮递员。

I send messages with postman.

POST https://android.googleapis.com/gcm/send
headers
Content-Type:application/json
Authorization:key= keyFromGoogleDevelopersConsole
{
  "to" : "/topics/global",
  "data":{
      "message": "Hello world!!!"
  }
}

发送后,我收到200 OK和消息ID,但手机没收到任何消息。

After send I receive 200 OK and message ID, but phone did not receive any messages.

我在做什么是错的?如何收到我的消息?

What I am doing is wrong? How receive my messages?

我已经改变了SenderID从开发者控制台电话号码,但它并没有帮助我。在调试控制台ASLO我注意到错误:

I have changed SenderID to numbers from Developers Console but it did not help me. Aslo I have noticed Errors in debug console:

11-21 17:32:58.014  31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.app.Notification$BigTextStyle', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
11-21 17:32:58.024  31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzap
11-21 17:32:58.027  31813-31813/gcm_test.app E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb

我觉得这个服务,retrives的消息需要以某种方式登记或启动。但是,当我运行它喜欢简单的服务崩溃。

I thought this service that retrives the messages need somehow register or start. But when I run it like simple service it crashes.

推荐答案

看起来你是不是用正确的发件人ID注册。该文档解释说,发件人ID是在谷歌开发者列出的数值项目编号控制台应用的项目。您正在使用发件人ID字符串, GCM测试-XXXXX ,貌似项目ID。

It looks like you are not registering with the correct Sender ID. The documentation explains that the Sender ID is the numerical Project Number listed at the Google Developers Console for your app's project. The string you are using for Sender ID, gcm-test-xxxxx, looks like the Project ID.

在在开发者控制台Dashboard页面,您的项目数据将包含这样一行:

On the Dashboard page at the Developers Console, your project data will include a line like this:

ID: jade-segment-123 (#123123123123)

有关本实施例中,发件人ID是123123123123

For this example, the Sender ID is 123123123123.

更新
还请注意,您不使用在文档所示的网址:

https://gcm-http.googleapis.com/gcm/send

这篇关于GCM的Andr​​oid 4.0.4不能得到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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