Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Firebase管理员SDK推送通知_C#_Firebase_Firebase Cloud Messaging - Fatal编程技术网

C#Firebase管理员SDK推送通知

C#Firebase管理员SDK推送通知,c#,firebase,firebase-cloud-messaging,C#,Firebase,Firebase Cloud Messaging,我想向移动应用发送推送通知,所以我在.Net项目中安装了Firebase Admin SDK。 我遵循了Firebase文档上的说明,但是当我从SDK调用SendAsync时,它只是挂起。知道它为什么会挂起来吗?我是否错过了脚步 这是我的API代码(请注意,这只是让它工作的演示代码): 由于SendAsync永远不会完成,因此出现死锁。 如果您是从UI线程调用的,您需要考虑使用 ConfigureAwait(false)< /代码> 因此,您的代码片段如下所示: public async v

我想向移动应用发送推送通知,所以我在.Net项目中安装了Firebase Admin SDK。 我遵循了Firebase文档上的说明,但是当我从SDK调用
SendAsync
时,它只是挂起。知道它为什么会挂起来吗?我是否错过了脚步

这是我的API代码(请注意,这只是让它工作的演示代码):


由于SendAsync永远不会完成,因此出现死锁。 如果您是从UI线程调用的,您需要考虑使用<强> <代码> ConfigureAwait(false)< /代码> <强> 因此,您的代码片段如下所示:

public async void SendPushNotification()
    {
        FirebaseApp.Create(new AppOptions()
        {
            Credential = GoogleCredential.FromFile("path to json file"),
        });

        var registrationToken = "fqCo_-tXKvY:APA91bGQ47Q2egnqn4Ml...";

        var message = new FirebaseAdmin.Messaging.Message()
        {
            Token = registrationToken,
        };

        string response = await FirebaseMessaging.DefaultInstance.SendAsync(message).ConfigureAwait(false);

        Console.WriteLine("Successfully sent message: " + response);
    }

firebaseAdmin消息传递在.net库中引发令牌错误,因此我使用Postman和works进行了测试,我能够发送通知,我看到Postman为Resharper库生成了一个代码,易于安装和使用,最好使用Json序列化消息并作为参数发送

using Firebase.Auth;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using Google.Apis.Auth.OAuth2;
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FirebaseAuth = FirebaseAdmin.Auth.FirebaseAuth;
using FirebaseAdmin.Messaging;
using RestSharp;
using Newtonsoft.Json;

namespace messaging
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {

            string token = "dtPMeTshqr??????????????";


            var data = new
            {
                to = token,
                notification = new
                {
                    body = "Nueva notificacion de prueba",
                    title = "Notificacion de prueba",
                },
                priority = "high"
            };

            var json = JsonConvert.SerializeObject(data);

            Sendmessage(json);

        }

        private static string Sendmessage(string json)
        {
            var client = new RestClient("https://fcm.googleapis.com/fcm/send");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "key=AAAAexjc2Qg:APx??????????????");
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            return response.Content;
        } 
    }
}

谢谢我会尝试一下,然后回复,这对我不起作用。我从未访问过success writeline。您是否设置了您的注册令牌和凭证文件p.s我已安装以下软件包
https://nugetmusthaves.com/Package/FirebaseAdmin
1.13.0版如何使用admin SDK创建firebase令牌?我正在尝试在asp.net MVC中实现推送通知。我从哪里获得注册令牌?
using Firebase.Auth;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using Google.Apis.Auth.OAuth2;
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FirebaseAuth = FirebaseAdmin.Auth.FirebaseAuth;
using FirebaseAdmin.Messaging;
using RestSharp;
using Newtonsoft.Json;

namespace messaging
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {

            string token = "dtPMeTshqr??????????????";


            var data = new
            {
                to = token,
                notification = new
                {
                    body = "Nueva notificacion de prueba",
                    title = "Notificacion de prueba",
                },
                priority = "high"
            };

            var json = JsonConvert.SerializeObject(data);

            Sendmessage(json);

        }

        private static string Sendmessage(string json)
        {
            var client = new RestClient("https://fcm.googleapis.com/fcm/send");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "key=AAAAexjc2Qg:APx??????????????");
            request.AddHeader("Content-Type", "application/json");
            request.AddParameter("application/json", json, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            return response.Content;
        } 
    }
}