简体   繁体   English

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

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

I'm writing VB.NET code to send notification to android Emulator.我正在编写 VB.NET 代码来向 android 模拟器发送通知。 I can successfully send test message from the firebase control.我可以从 firebase 控件成功发送测试消息。 However, it failed when I tried to send message via VB.NET code in my local machine and get error "The remote server returned an error: (401) Unauthorized".但是,当我尝试在本地计算机上通过 VB.NET 代码发送消息并收到错误消息“远程服务器返回错误:(401) 未经授权”时,它失败了。

I have tried looking at the following link: FCM (Firebase Cloud Messaging) Push Notification with Asp.Net and following the instructions but it still not working.我尝试查看以下链接: FCM (Firebase Cloud Messaging) Push Notification with Asp.Net并按照说明进行操作,但它仍然无法正常工作。

Here is my code:这是我的代码:

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


The error occurred at the line:错误发生在以下行:

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

Here is the server key and sender ID that I use:这是我使用的服务器密钥和发件人 ID: 在此处输入图像描述

Updated: I tried sending web request from Postman application and it also gave the same error (401: Unauthorized) as shown in the figure below:更新:我尝试从 Postman 应用程序发送 Web 请求,它也给出了相同的错误(401:未经授权),如下图所示:

发布 Web 请求失败

OK.好的。 I made a silly mistake by using API Key which is not actual Server key.我通过使用不是实际服务器密钥的 API 密钥犯了一个愚蠢的错误。 Even though I have read that I should go to cloud messaging tab in project setting, I misunderstood that I have to look in cloud messaging tab in the side bar which is wrong.尽管我已经读到我应该去项目设置中的云消息选项卡,但我误解了我必须查看侧栏中的云消息选项卡,这是错误的。 Here is the step where I found server key:这是我找到服务器密钥的步骤:

  1. Click on the setting button near Project Overview单击项目概述附近的设置按钮

    在此处输入图像描述

  2. Choose the first option (Project setting)选择第一个选项(项目设置)

    在此处输入图像描述

  3. Click on the second tab (Cloud messaging)单击第二个选项卡(云消息传递)

    在此处输入图像描述

  4. Use the server key and sender ID in this page在此页面中使用服务器密钥和发件人 ID

    在此处输入图像描述

At first, I tried looking for server key in cloud messaging tab in the sidebar which is wrong location.起初,我尝试在侧边栏的云消息选项卡中查找服务器密钥,但这是错误的位置。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 VB.Net 中检索多个数据 Firebase - Retrive Multiple Data Firebase in VB.Net 通知已成功发送,但未从 Firebase 在 Android 中接收 - Notification sent successfully but not reveiving in Android from Firebase Android 发送一个信号通知时应用程序崩溃 - Android App Crash when One Signal Notification sent 是否有在 .net 上使用 AWS SDK 连接到 aws vpn 的代码示例? C# 或 VB.NET - Is there any code example of connect into aws vpn with AWS SDK on .net? C# or VB.NET 未收到通过 iOS 应用程序中的 firebase 云功能发送的推送通知,尽管我从消息控制台获取 - Not receiving push notification sent via firebase cloud functions in iOS app though I get the from messaging console 如何使用 firebase 在 android 中的通知中发送图像? - how to send image in notification in android using firebase? firebase 推送通知“错误”:“无效注册”使用 codeigniter - firebase push notification "error": "InvalidRegistration" using codeigniter Flutter - Firebase 使用 firebase 消息传递成功但未收到通知的推送通知 - Flutter - Firebase push notification using firebase messaging success but not get notification Firebase 可调用云 Function CORS 错误使用 Z035489FF8D092741943E4A83241F5 - Firebase Callable Cloud Function CORS Error using Firebase Emulator and Vite 如何使用 Amazon SES 获取 Email Sent 的副本? - How to get copy of Email Sent using Amazon SES?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM