I was sending notifications from my ASP.NET MVC project, but now I can't send them anymore. I get a mismatched sender id error. I checked my sender id and it's correct.
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.
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.