使用FirebaseAdmin = NullReferenceException发送推送通知

时间:2019-06-11 13:20:34

标签: c# firebase-cloud-messaging firebase-admin

我正在尝试让FirebaseAdmin进入我的.Net Web.Forms C#项目,并且一直在获取NullReferenceExceptions。我已使用此链接(FCM (Firebase Cloud Messaging) Push Notification with Asp.Net)作为参考(2019年编辑),但没有用。然后,我通过Application_Start方法将SDK初始化移至Global.asax,并尝试使用DefaultInstance发送我的请求(我也看到过使用该方法)。

我的代码:

Global.asax

  protected void Application_Start(object sender, EventArgs e)
        {

            // Enable firebase
            FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromFile(HostingEnvironment.MapPath(@"~/App_Data/xxxx-11788e8acbe2.json"))
            });
}

我发送推送的班级:

namespace MyApp.classes
{
    public class FCM
    {

        public FCM() { }


        private FirebaseAdmin.Messaging.Message CreateNotificationForTopic(string title, string notificationBody, string topic)
        {
            return new FirebaseAdmin.Messaging.Message()
            {
                Topic = topic,
                Notification = new Notification()
                {
                    Body = notificationBody,
                    Title = title
                }
            };
        }



        public async Task SendNotificationToTopic(string topic, string title, string body)
        {
            try
            {
                var result = await FirebaseMessaging.DefaultInstance.SendAsync(CreateNotificationForTopic(title, body, topic));
            }
            catch (Exception x)
            {
                string xxx = x.Message;
            }
        }
    }
}

很难修复,因为在运行SendNotificationToTopic时我只是得到了一个N​​ullReferenceException而没有任何其他信息。有人有任何想法吗?非常感谢。

PS:FirebaseMessage不为空:

enter image description here

PS2:DefaultInstance有一个值,但是未设置ProjectId吗?可以吗?但是它存在于初始化SDK时使用的Json文件中吗?

enter image description here

答案:好的,我知道了。非常感谢@mjwills抽出宝贵的时间来提供帮助。由于某种原因,该线程过早关闭。问题在于,服务帐户控制台中已经以某种方式生成了两个项目,并且当然是针对未使用的应用程序创建了SDK。

我在正确的项目下创建了一个新的服务帐户,下载了凭据和中提琴,它可以正常工作。

0 个答案:

没有答案
相关问题