GCM 问题(谷歌云消息)

Posted

技术标签:

【中文标题】GCM 问题(谷歌云消息)【英文标题】:GCM Questions (Google Cloud Messaging) 【发布时间】:2012-07-27 10:37:19 【问题描述】:

我有几个关于 GCM(Google 云消息传递)的问题。

第一个问题是:我无法启动程序。它似乎总是崩溃:

清单:

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

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

    <permission android:name="com.example.gcmtutorial.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcmtutorial.permission.C2D_MESSAGE" /> 
<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" >

          <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="com.example.gcmtutorial" />
              </intent-filter>
            </receiver>

        <service android:name=".GCMIntentService" />
        <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>

    </application>

</manifest>

代码是:

package com.example.gcmtutorial;

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);
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);

        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) 
          GCMRegistrar.register(this, "714669202278");
         else 
          Log.v("Registered", "Already registered");
        

        setContentView(R.layout.activity_main);
    

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

我收到的错误是

07-27 18:11:23.504: E/AndroidRuntime(11390): FATAL EXCEPTION: main
07-27 18:11:23.504: E/AndroidRuntime(11390): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.gcmtutorial/com.example.gcmtutorial.MainActivity: java.lang.SecurityException: Not allowed to start service Intent  act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras)  without permission com.google.android.c2dm.permission.RECEIVE
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.os.Looper.loop(Looper.java:156)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread.main(ActivityThread.java:4987)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at dalvik.system.NativeStart.main(Native Method)
07-27 18:11:23.504: E/AndroidRuntime(11390): Caused by: java.lang.SecurityException: Not allowed to start service Intent  act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras)  without permission com.google.android.c2dm.permission.RECEIVE
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ContextImpl.startService(ContextImpl.java:1356)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.content.ContextWrapper.startService(ContextWrapper.java:359)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at com.google.android.gcm.GCMRegistrar.internalRegister(GCMRegistrar.java:229)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:217)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at com.example.gcmtutorial.MainActivity.onCreate(MainActivity.java:24)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.Activity.performCreate(Activity.java:4538)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-27 18:11:23.504: E/AndroidRuntime(11390):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-27 18:11:23.504: E/AndroidRuntime(11390):    ... 11 more

我已经谷歌了,但似乎没有解决方案。

我的第二个问题是: 如何使用 GCM 编写 php 服务器 我提到过: GCM sending with curl (php) GCM with PHP (Google Cloud Messaging)

我不明白的是注册 ID 是什么意思

【问题讨论】:

您的清单看起来不错。您是否尝试卸载该应用,然后重新安装? 【参考方案1】:

在您的清单中尝试使用 package.GCMIntentService

【讨论】:

以上是关于GCM 问题(谷歌云消息)的主要内容,如果未能解决你的问题,请参考以下文章

谷歌云消息传递 GCM 可靠吗?

GCM(谷歌云消息)无法覆盖所有注册用户?

如何使用谷歌云连接服务器发送上游 GCM 消息

无需谷歌云消息(GCM)将通知从 PC 推送到 android

关于 GCM 中的规范 ID(谷歌云消息传递)

适用于 Android 谷歌云消息 (GCM) 的 Azure 移动服务从不向设备发送通知