繁体   English   中英

GCM Android 4.0.4 无法获取消息

[英]GCM Android 4.0.4 cant get message

我遵循了来自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 中的数字,但这对我没有帮助。 Aslo我注意到调试控制台中的错误:

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

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

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

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

ID: jade-segment-123 (#123123123123)

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

更新:另请注意,您没有使用 文档中显示的 URL:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM