gpt4 book ai didi

android - 使用VB.NET向android模拟器发送通知得到错误401

转载 作者:行者123 更新时间:2023-11-29 02:21:08 26 4
gpt4 key购买 nike

我正在编写 VB.NET 代码来向 Android 模拟器发送通知。我可以从 firebase 控件成功发送测试消息。但是,当我尝试在本地计算机上通过 VB.NET 代码发送消息并收到错误消息“远程服务器返回错误:(401) 未经授权”时,它失败了。

我试过查看以下链接: FCM (Firebase Cloud Messaging) Push Notification with Asp.Net并按照说明进行操作,但仍然无法正常工作。

这是我的代码:

Imports System.Net
Imports Newtonsoft.Json

Public Class Notification

Public Sub SendNotification(ByVal deviceIDList As List(Of String), ByVal title As String, ByVal bodyMsg As String)

Dim fcmPath As String = "https://fcm.googleapis.com/fcm/send"
Dim serverKey As String = "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoCAeI"
Dim senderID As String = "35xxxxxxx37"

Dim request As HttpWebRequest = CType(HttpWebRequest.Create(fcmPath), HttpWebRequest)
With request
.Method = "POST"
.ContentType = "application/json"
.Headers.Add(String.Format("Authorization: key={0}", serverKey))
.Headers.Add(String.Format("Sender: id={0}", senderID))
End With

Using streamWriter = New StreamWriter(request.GetRequestStream())
Dim webObject As New WebRequestFcmData
With webObject
.registration_ids = deviceIDList
.notification.title = title
.notification.body = bodyMsg
.notification.content_available = True
.notification.sound = "default"
.notification.priority = "high"
End With
Dim body = JsonConvert.SerializeObject(webObject)
With streamWriter
.Write(body)
.Flush()
.Close()
End With
End Using
Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)
Using streamReader As New StreamReader(httpResponse.GetResponseStream)
Dim result = streamReader.ReadToEnd
End Using
End Sub
End Class

Public Class WebRequestFcmData
Public Property registration_ids As List(Of String)
Public Property notification As New NotificationData
End Class

Public Class NotificationData
Public Property body As String
Public Property content_available As Boolean
Public Property priority As String
Public Property title As String
Public Property sound As String
End Class


错误发生在以下行:

Dim httpResponse = CType(request.GetResponse(), HttpWebResponse)

这是我使用的服务器 key 和发件人 ID: enter image description here

更新:我尝试从 Postman 应用程序发送 Web 请求,它也给出了相同的错误(401:未经授权),如下图所示:

Posting Web Request Failed

最佳答案

好的。我通过使用不是实际服务器 key 的 API key 犯了一个愚蠢的错误。尽管我已经读到我应该去项目设置中的云消息选项卡,但我误解了我必须查看侧栏中的云消息选项卡,这是错误的。这是我找到服务器 key 的步骤:

  1. 点击项目概览旁边的设置按钮

    enter image description here

  2. 选择第一个选项(项目设置)

    enter image description here

  3. 点击第二个标签(云消息)

    enter image description here

  4. 在此页面中使用服务器 key 和发件人 ID

    enter image description here

起初,我尝试在侧边栏的云消息选项卡中查找服务器 key ,但位置错误。

enter image description here

关于android - 使用VB.NET向android模拟器发送通知得到错误401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55589936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com