ฉันกำลังเขียนโค้ด VB.NET เพื่อส่งการแจ้งเตือนไปยัง android Emulator ฉันสามารถส่งข้อความทดสอบจากการควบคุม firebase ได้สำเร็จ อย่างไรก็ตาม มันล้มเหลวเมื่อฉันพยายามส่งข้อความผ่านโค้ด VB.NET ในเครื่องของฉันและได้รับข้อผิดพลาด "เซิร์ฟเวอร์ระยะไกลส่งคืนข้อผิดพลาด: (401) ไม่ได้รับอนุญาต"
ฉันได้ลองดูลิงค์ต่อไปนี้: FCM (Firebase Cloud Messaging ) Push Notification ด้วย 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)
นี่คือรหัสเซิร์ฟเวอร์และ ID ผู้ส่งที่ฉันใช้:
อัปเดต: ฉันพยายามส่งคำขอเว็บจากแอปพลิเคชันบุรุษไปรษณีย์ และยังให้ข้อผิดพลาดเดียวกัน (401: ไม่ได้รับอนุญาต) ดังแสดงในรูปด้านล่าง: