简体   繁体   English

通知未在 ASP.NET MVC 上发送

[英]Notifications not sending on ASP.NET MVC

I was sending notifications from my ASP.NET MVC project, but now I can't send them anymore.我从我的 ASP.NET MVC 项目发送通知,但现在我不能再发送它们了。 I get a mismatched sender id error.我收到不匹配的发件人 ID 错误。 I checked my sender id and it's correct.我检查了我的发件人 ID,它是正确的。

Sample response示例响应

{ 
    "multicast_id": 5340432438815499122,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [ {"error":"MismatchSenderId"} ] 
}

I am using this code;我正在使用这段代码;

public string ExecutePushNotification(string title, string msg, string fcmToken, object data)
{
    var serverKey = "AAAAxxxx ";
    var senderId = "xxxxxx";
    var result = "-1";

    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
    httpWebRequest.ContentType = "application/x-www-urlencoded";
    httpWebRequest.Headers.Add(string.Format("Authorization: key={0}", serverKey));
    httpWebRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
    httpWebRequest.Method = "POST";

    var payload = new
        {
            notification = new
            {
                title = title,
                body = msg,
                sound = "default"
            },
            data = new
            {
                info = data
            },
            to = fcmToken,
            priority = "high",
            content_available = true,
        };

    var serializer = new JavaScriptSerializer();

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = serializer.Serialize(payload);
        streamWriter.Write(json);
        streamWriter.Flush();
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        result = streamReader.ReadToEnd();
    }

    return result;
}

Any suggestions?有什么建议么?

I fixed it by Adding Firebase Admin package for ASP.NET and google.json file.我通过为 ASP.NET 和 google.json 文件添加 Firebase Admin package 来修复它。

public class FCMResponse
    {
        public long multicast_id { get; set; }
        public int success { get; set; }
        public int failure { get; set; }
        public int canonical_ids { get; set; }
        public List<FCMResult> results { get; set; }
    }
    public class FCMResult
    {
        public string message_id { get; set; }
    }

    public string ExecutePushNotification(string title, string msg, string fcmToken, object data)
    {

        if (FirebaseApp.DefaultInstance == null)
        {
            var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "google-services.json");


            var defaultApp = FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "varmallapp-firebase-adminsdk-9yv7a-46e9351df3.json")),
            });
        }
        else
        {
            var defaultApp = FirebaseApp.DefaultInstance;
        }
        
    
     //   Console.WriteLine(defaultApp.Name); // "[DEFAULT]"
        var message = new FirebaseAdmin.Messaging.Message()
        {
            Data = new Dictionary<string, string>()
            {
                ["Date"] = DateTime.Now.ToString(),
                ["User"] = "VARBAZAR"
            },
            Notification = new FirebaseAdmin.Messaging.Notification
            {
                Title = title,
                Body = msg
            },


            Token = fcmToken
          
        };
        var messaging = FirebaseMessaging.DefaultInstance;
        var result = messaging.SendAsync(message);
        //Console.WriteLine(result); //projects/myapp/messages/2492588335721724324
        return result.ToString();

    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM