4

我正在尝试通过 GCM 将推送消息传递到我在调试模式下本地连接到桌面的 Android 设备。我可以在 GCM 上注册并取回注册 ID。但是,当我从服务器向 GCM 发送消息时,它永远不会到达我的设备。任何想法这里缺少什么?

这是我用来向 GCM 发送消息的 C# 代码:

                wc.Headers.Add("Authorization", "key=" + senderAuthToken);
                var nameValues = new NameValueCollection
                                     {
                                         {"registration_id", registrationId},
                                         {"collapse_key", Guid.NewGuid().ToString()},
                                         {"data.payload", HttpUtility.UrlEncode(message)}
                                     };
                var resp = wc.UploadValues("https://android.googleapis.com/gcm/send", nameValues);
                var respMessage = Encoding.Default.GetString(resp);

这是我从 GCM 得到的回复:

id=0:1345244000449737%276b04adef6fbb69

所以看起来消息正在验证并在 GCM 中排队。

这是我的 AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fiserv.mobile.poc"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="15" />

<permission android:name="fiserv.mobile.poc.permission.C2D_MESSAGE" 
    android:protectionLevel="signature"/>

<uses-permission android:name="fiserv.mobile.poc.permission.C2D_MESSAGE" /> 

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" /> 
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />  

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <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.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" />
        <category android:name="fiserv.mobile.poc" />
      </intent-filter>
    </receiver>

    <service android:name=".GCMIntentService" />

</application>

这是我的 GCMIntentService.java 服务:

package fiserv.mobile.poc;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {

    @Override
    protected void onError(Context arg0, String arg1) {
        Log.e("Registration", "Got an error!");
        Log.e("Registration", arg0.toString() + arg1.toString());
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.i("Registration", "Got a message!");
    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        Log.i("Registration", "Just registered!");
        Log.i("Registration", arg0.toString() + arg1.toString());   

    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
    }
}

最后是我的 MainActivity.java:

package fiserv.mobile.poc;

import fiserv.mobile.poc.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RegisterWithGCM();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void RegisterWithGCM()
    {           
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, SENDER_ID);
        } else {

          Log.v("Registration", "Already registered, regId: " + regId);
        }
    }

    String SENDER_ID = "158778556981";
}

当我向 GCM 注册时,我可以看到日志语句,但onMessage无论我向 GCM 发送多少消息,我都没有点击或看到日志语句“收到消息!”。我已验证我正在使用与我在 Android 应用程序中使用的“发件人 ID”匹配的服务密钥。

任何想法为什么没有传递消息?

4

1 回答 1

3

您必须有一个 Google 帐户登录到您的设备。我没有收到通知的原因是因为我在开始测试 GMC 之前更改了我的 Google (Gmail) 密码。更新密码后,我开始在我的设备上收到通知。

在 android 2.2 上,设置 > 帐户部分没有“更新密码”按钮。我不得不去 Play 应用程序然后尝试下载一个应用程序,然后它要求输入新密码。

于 2012-08-20T16:11:56.893 回答