1

我正在尝试为我的应用程序实现推送通知。首先,我使用了旧的/已弃用的 DemoAcitivity 示例,并且我成功地从服务器接收到我的应用程序的消息。

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

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

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

私人无效注册背景(){

    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>

有任何想法吗?

4

1 回答 1

-1

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

<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,所以您可能正在使用高级功能。

于 2013-07-08T12:45:03.107 回答