【问题标题】:How to code for push notification in android using c#?如何使用 C# 在 android 中编写推送通知的代码?
【发布时间】:2015-03-16 05:49:46
【问题描述】:

我是 android 应用程序开发的新手,我需要为具有推送通知功能的应用程序编写代码,我需要为应该与 mySQL DB 连接的推送通知编写代码...请帮助

【问题讨论】:

    标签: c# android xamarin push-notification


    【解决方案1】:

    使用 .net c# 代码将通知从服务器发送到设备

    参考:http://www.codeproject.com/Tips/434338/Android-GCM-Push-Notification

    public class AndroidGCMPushNotification
    {
        public AndroidGCMPushNotification()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public string SendNotification(string deviceId, string message)
        {
            string GoogleAppID = "google application id";        
            var SENDER_ID = "9999999999";
            var value = message;
            WebRequest tRequest;
            tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
    
            tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
    
            string postData = "collapse_key=score_update&time_to_live=108&
            delay_while_idle=1&data.message=" + value + "&data.time=" + 
            System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
            Console.WriteLine(postData);
            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            tRequest.ContentLength = byteArray.Length;
    
            Stream dataStream = tRequest.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
    
            WebResponse tResponse = tRequest.GetResponse();
    
            dataStream = tResponse.GetResponseStream();
    
            StreamReader tReader = new StreamReader(dataStream);
    
            String sResponseFromServer = tReader.ReadToEnd();
    
            tReader.Close();
            dataStream.Close();
            tResponse.Close();
            return sResponseFromServer;
        }
    }
    

    处理设备中的消息

    https://developer.android.com/google/gcm/index.html

    【讨论】:

    • 感谢您的建议
    【解决方案2】:

    快速入门的方法是使用 Google Cloud Messaging。

    https://developer.android.com/google/gcm/index.html

    【讨论】:

    • 感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多