GoogleCloudMessaging.register(...)方法始终返回SERVICE_NOT AVAILABLE

时间:2013-07-05 13:14:45

标签: android push-notification google-cloud-messaging

我正在尝试为我的应用程序实现推送通知。首先,我使用旧的/ depracated DemoAcitivity样本,我成功地从服务器接收消息到我的应用程序。

由于它已被弃用,我尝试了新的GCM API以实现相同的Push Notif。服务。在此实现过程中,我主要使用此处提供的代码;

   http://developer.android.com/google/gcm/gs.html

在我的代码中,每次尝试将设备注册到GCM时,都会收到SERVICE_NOT AVAILABLE错误。我用来注册的方法正好是文档中的smae(上面链接);

private void registerBackground(){

    new AsyncTask<Void, Object, String>() {

        @Override
        protected String doInBackground(Void... objects) {

            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }                  

                regid = gcm.register(GenericUtilities.SENDER_ID);
                msg = "Device registered, registration id=" + regid;


                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the message
                // using the 'from' address in the message.

                // Save the regid - no need to register again.
                setRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();

            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append((String)msg.toString() + "\n");
        }

    }.execute(null, null, null);

}

这就是明显。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pushnotificationexample"
android:versionCode="1"
android:versionName="1.0" >

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

<!-- 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" />

<!--
 Creates a custom permission so only this app can receive its messages.

 NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
       where PACKAGE is the application's package name.
-->
<permission
    android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="com.example.pushnotificationexample.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission
    android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >        

    <activity
        android:name="com.example.pushnotificationexample.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.example.pushnotificationexample.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.pushnotificationexample" />
        </intent-filter>
    </receiver>               

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

    </application>

</manifest>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我不确定它是否能解决您的问题,但不再需要以下行,应该从您的清单中删除:

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

此外,您不再需要以下行(尽管它不应该与您获得的错误有任何关系):

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

8/23/13编辑:

当我使用API​​ 8构建我的应用程序并在我的清单中声明时:

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

我没有收到此错误。

使用API​​ 10构建应用程序并将清单更改为:

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

我收到了这个错误。我不知道它是否有帮助,因为你的targetSdkVersion是17,所以你可能正在使用高级功能。