如何实现在Android的推送通知

如何实现在Android的推送通知

本文介绍了如何实现在Android的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我从一天多尝试使用GCM实现一个简单的推送通知,但无法执行它。

在以往我加入这个权限

 <允许机器人:名字=Android.Test.permission.C2D_MESSAGE安卓的ProtectionLevel =签名/>

而通过移动设备调试,我得到这个错误讯息

  PKG:/data/local/tmp/Android.Test
失败[INSTALL_PARSE_FAILED_MANIFEST_MALFORMED]

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

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          包=Android.Test
          安卓版code =1
          机器人:=的versionName1.0>
    <采用-SDK安卓的minSdkVersion =14/>    <允许机器人:名字=Android.Test.permission.C2D_MESSAGE安卓的ProtectionLevel =正常/>
    <使用许可权的android:NAME =Android.Test.permission.C2D_MESSAGE/>
    <使用许可权的android:NAME =android.permission.GET_ACCOUNTS/>
    <使用许可权的android:NAME =android.permission.WAKE_LOCK/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =com.google.android.c2dm.permission.RECEIVE/>    <应用机器人:标签=@字符串/ APP_NAME机器人:图标=@绘制/ ic_launcher>
        <活动机器人:名字=MainActivity
                  机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <接收
                机器人:名字=com.google.android.gcm.GCMBroadcastReceiver
                机器人:权限=com.google.android.c2dm.permission.SEND>
            &所述;意图滤光器>
                <作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/>
                <作用机器人:名字=com.google.android.c2dm.intent.REGISTRATION/>                <类机器人:名字=Android.Test/>
            &所述; /意图滤光器>
        < /接收器>
        <服务机器人。GCMIntentServiceNAME = />
    < /用途>
< /清单>

编辑1:
这是我想从C#发送邮件到我的Andr​​oid设备.....我发现从这个博客code。

 私人字符串SendGCMNotification(字符串apiKey,串POSTDATA,串postDataContentType =应用/ JSON)
        {            ServicePointManager.ServerCertificateValidationCallback + =新RemoteCertificateValidationCallback(ValidateServerCertificate);            //
            //邮件内容
            字节[]的字节数组= Encoding.UTF8.GetBytes(POSTDATA);            //
            //创建请求
            HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(https://android.googleapis.com/gcm/send);
            Request.Method =POST;
            Request.KeepAlive = FALSE;
            Request.ContentType = postDataContentType;
            Request.Headers.Add(的String.Format(授权:键= {0},apiKey));
            Request.ContentLength = byteArray.Length;            流数据流= Request.GetRequestStream();
            dataStream.Write(字节数组,0,byteArray.Length);
            dataStream.Close();            //
            // 发信息
            尝试
            {
                WebResponse的响应= Request.GetResponse();
                的HTTPStatus code响应code =((HttpWebResponse)响应).STATUS code;
                如果(响应code.Equals(的HTTPStatus code.Unauthorized)||响应code.Equals(的HTTPStatus code.Forbidden))
                {
                    VAR文本=未经授权 - 需要新的令牌;                }
                否则,如果(!响应code.Equals(的HTTPStatus code.OK))
                {
                    VAR文本=从Web服务响应不OK;
                }                StreamReader的读者=新的StreamReader(Response.GetResponseStream());
                字符串responseLine = Reader.ReadToEnd();
                Reader.Close();                返回responseLine;
            }
            赶上(例外五)
            {            }
            返回错误;
        }

这是我在responseLine code正在逐渐

<$p$p><$c$c>{\"multicast_id\":7357801824672,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"}]}

感谢


解决方案

 试试这个


 &LT;采用-SDK安卓的minSdkVersion =8机器人:targetSdkVersion =16/&GT;
&LT;使用许可权的android:NAME =android.permission.INTERNET对/&GT;
&LT;使用许可权的android:NAME =android.permission.GET_ACCOUNTS/&GT;
&LT;使用许可权的android:NAME =android.permission.WAKE_LOCK/&GT;
&LT;许可
    机器人:名字=com.google.android.gcm.demo.app.permission.C2D_MESSAGE
    安卓的ProtectionLevel =签名/&GT;
&LT;使用许可权
    机器人:名字=com.google.android.gcm.demo.app.permission.C2D_MESSAGE/&GT;&LT;! - 这个程序有权限注册和接收数据信息。 - &GT;
&LT;使用许可权
    机器人:名字=com.google.android.c2dm.permission.RECEIVE/&GT;&LT;! - 主要活动。 - &GT;
&lt;应用
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME&GT;
    &LT;活动
        机器人:名字=。MainActivity
        机器人:标签=@字符串/ APP_NAME
        机器人:screenOrientation =肖像&GT;
        &所述;意图滤光器&gt;
            &lt;作用机器人:名字=android.intent.action.MAIN/&GT;            &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
        &所述; /意图滤光器&gt;
    &LT; /活性GT;
    &lt;接收
        机器人:名字=com.google.android.gcm.GCMBroadcastReceiver
        机器人:权限=com.google.android.c2dm.permission.SEND&GT;
        &所述;意图滤光器&gt;
            &LT;! - 收到实际的消息。 - &GT;
            &lt;作用机器人:名字=com.google.android.c2dm.intent.RECEIVE/&GT;
            &LT;! - 收到的注册ID。 - &GT;
            &lt;作用机器人:名字=com.google.android.c2dm.intent.REGISTRATION/&GT;
            &LT;类机器人:名字=Android.Test/&GT;
        &所述; /意图滤光器&gt;
    &LT; /接收器&GT;
    &LT;服务机器人。GCMIntentServiceNAME = /&GT;
&LT; /用途&gt;

I am trying from more than a day to implement a simple PUSH notification using gcm, but unable to implement it.

When ever I am adding this permission

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

I get this error message while debugging via mobile device

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

Can somebody suggest me what I am doing. for further help I am putting my whole manifest.xml file

<?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>

Edit 1:This is how I am trying to send the message from C# to my android device.....I found this code from the blog.

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";
        }

This is what I am getting in the responseLine code

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

Thanks

解决方案
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>

这篇关于如何实现在Android的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:58