GCM Android 4.0.4无法获取消息

时间:2015-11-20 18:53:21

标签: java android google-cloud-messaging

我按照developers.google.com中的示例创建了用于从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

开始这个课程作为服务
    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检索邮件的课程。这个课程根本不起作用。

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

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

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

我用邮递员发送邮件。

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,但是手机没有收到任何消息。

我在做什么是错的?如何收到我的留言?

我已将SenderID更改为Developers 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

我认为这个重新发送消息的服务需要以某种方式注册或启动。但当我像简单的服务一样运行它时会崩溃。

1 个答案:

答案 0 :(得分:0)

看起来您没有使用正确的发件人ID进行注册。 documentation解释了发件人ID是Google Developers Console中为您的应用项目列出的数字项目编号。您用于发件人ID的字符串gcm-test-xxxxx看起来像项目ID。

在Developers Console的Dashboard页面上,您的项目数据将包含如下所示的行:

ID: jade-segment-123 (#123123123123)

对于此示例,发件人ID为123123123123。

<强>更新: 另请注意,您没有使用documentation中显示的网址:

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