【问题标题】:send push notifications in android using firebase使用firebase在android中发送推送通知
【发布时间】:2017-06-10 12:30:09
【问题描述】:

我有三个应用程序。使用 Asp.NET MVC、Asp.NET Web API 应用程序和 Android 应用程序开发的内容管理系统。我使用 SQL Server 2016 作为我的数据库。内容管理系统和 Android 应用程序通过 Web API 执行数据库操作。

现在我需要在我的 Android 应用程序中实现推送通知。每当管理员从内容管理系统发布新数据时,Android 应用程序用户应该会在他们的手机中收到推送通知。

我该如何实现呢?我在网上浏览了各种教程。他们已经使用 Firebase 实现了。我明白那一点。但他们也使用 php 脚本和 WAMP 服务器来实现。我对php不熟悉。

有人能告诉我如何在不使用 php 的情况下在我的场景中实现推送通知吗?非常感谢您的帮助。

谢谢

【问题讨论】:

    标签: android asp.net-mvc asp.net-web-api push-notification sql-server-2016


    【解决方案1】:

    ASP 端使用 FCM

    try
            {
                var applicationID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                var senderId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                string deviceId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = "This is the message Body",
                        title = "This is the title of Message",
                        icon = "myicon"
                    },
                    priority = "high"
    
                };
    
                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                tRequest.ContentLength = byteArray.Length;
    
                using (Stream dataStream = tRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
    
                    using (WebResponse tResponse = tRequest.GetResponse())
                    {
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        {
                            using (StreamReader tReader = new StreamReader(dataStreamResponse))
                            {
                                String sResponseFromServer = tReader.ReadToEnd();
                                Response.Write(sResponseFromServer);
                                Label3.Text = sResponseFromServer;
                            }
                        }
                    }
                }
            }
    
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
    
    
        }
    

    android端usinf FCM跟随链接

    https://www.codementor.io/flame3/send-push-notifications-to-android-with-firebase-du10860kb

    【讨论】:

    • 链接中的指南有效。但是我仍然不确定将上面的 ASP 代码放在哪里。我尝试在我的 Web API 中放置 Post 方法。但我不确定它是如何工作的。以及如何生成设备 ID 和发件人 ID。我是新手,请原谅我的无知。
    • 您可以在 android 类中生成设备 id,您可以在登录时发送该设备 id - 按照教程在 android 类中查找令牌 id,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多