簡體   English   中英

使用VB.NET向android模擬器發送通知得到錯誤401

[英]Using VB.NET to sent notification to android emulator get error 401

我正在編寫 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)

這是我使用的服務器密鑰和發件人 ID: 在此處輸入圖像描述

更新:我嘗試從 Postman 應用程序發送 Web 請求,它也給出了相同的錯誤(401:未經授權),如下圖所示:

發布 Web 請求失敗

好的。 我通過使用不是實際服務器密鑰的 API 密鑰犯了一個愚蠢的錯誤。 盡管我已經讀到我應該去項目設置中的雲消息選項卡,但我誤解了我必須查看側欄中的雲消息選項卡,這是錯誤的。 這是我找到服務器密鑰的步驟:

  1. 單擊項目概述附近的設置按鈕

    在此處輸入圖像描述

  2. 選擇第一個選項(項目設置)

    在此處輸入圖像描述

  3. 單擊第二個選項卡(雲消息傳遞)

    在此處輸入圖像描述

  4. 在此頁面中使用服務器密鑰和發件人 ID

    在此處輸入圖像描述

起初,我嘗試在側邊欄的雲消息選項卡中查找服務器密鑰,但這是錯誤的位置。

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM