0

我从一天多的时间开始尝试使用 gcm 实现一个简单的 PUSH 通知,但无法实现它。

每当我添加此权限时

<permission android:name="Android.Test.permission.C2D_MESSAGE"  android:protectionLevel="signature" />

通过移动设备进行调试时收到此错误消息

pkg: /data/local/tmp/Android.Test
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]

有人可以建议我在做什么。为了进一步的帮助,我把我的整个 manifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="Android.Test"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="14"/>

    <permission android:name="Android.Test.permission.C2D_MESSAGE"  android:protectionLevel="normal" />
    <uses-permission android:name="Android.Test.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />



    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <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.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="Android.Test" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />
    </application>
</manifest>

编辑 1:这就是我尝试将消息从 C# 发送到我的 android 设备的方式.....我从博客中找到了这段代码。

private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
        {

            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate);

            //
            //  MESSAGE CONTENT
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            //
            //  CREATE REQUEST
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
            Request.Method = "POST";
            Request.KeepAlive = false;
            Request.ContentType = postDataContentType;
            Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
            Request.ContentLength = byteArray.Length;

            Stream dataStream = Request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            //
            //  SEND MESSAGE
            try
            {
                WebResponse Response = Request.GetResponse();
                HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
                if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
                {
                    var text = "Unauthorized - need new token";

                }
                else if (!ResponseCode.Equals(HttpStatusCode.OK))
                {
                    var text = "Response from web service isn't OK";
                }

                StreamReader Reader = new StreamReader(Response.GetResponseStream());
                string responseLine = Reader.ReadToEnd();
                Reader.Close();

                return responseLine;
            }
            catch (Exception e)
            {

            }
            return "error";
        }

这就是我在 responseLine 代码中得到的

{"multicast_id":7357801824672,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

谢谢

4

2 回答 2

1
try this one       

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


<uses-permission android:name="android.permission.INTERNET" />


<uses-permission android:name="android.permission.GET_ACCOUNTS" />


<uses-permission android:name="android.permission.WAKE_LOCK" />


<permission
    android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission
    android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" />

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

<!-- Main activity. -->
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <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>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="Android.Test" />
        </intent-filter>
    </receiver>


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

于 2012-09-14T03:36:03.173 回答
0

您的响应代码包含答案,您传递的 registration_ids 无效

    "results":[{"error":"InvalidRegistration"}]

示例 php 代码:

    $postdata = array(
        'registration_ids' => array('your_device_registration_key'),
        'data' => 'message'
    );

    $headers = array(
        'Authorization: key=' . YOUR_API_KEY,
        'Content-Type: application/json'
    );
于 2013-02-15T16:12:21.177 回答