ASP.NET MVC 上未发送通知
Notifications not sending on ASP.NET MVC
我从我的 ASP.NET MVC 项目发送通知,但现在我不能再发送它们了。我收到不匹配的发件人 ID 错误。我检查了我的发件人 ID,它是正确的。
示例响应
{
"multicast_id": 5340432438815499122,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [ {"error":"MismatchSenderId"} ]
}
我正在使用此代码;
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;
}
有什么建议吗?
我通过为 ASP.NET 和 google .json 文件添加 Firebase Admin 包来修复它。
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();
}
}
我从我的 ASP.NET MVC 项目发送通知,但现在我不能再发送它们了。我收到不匹配的发件人 ID 错误。我检查了我的发件人 ID,它是正确的。
示例响应
{
"multicast_id": 5340432438815499122,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [ {"error":"MismatchSenderId"} ]
}
我正在使用此代码;
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;
}
有什么建议吗?
我通过为 ASP.NET 和 google .json 文件添加 Firebase Admin 包来修复它。
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();
}
}