1

我正在按照此处的指南进行操作,但是当我尝试在 Xamarin 表单 Andriod 中启动通知时,我得到以下信息。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications

INotificationManager notificationManager;
int notificationNumber = 0;


notificationManager = DependencyService.Get<INotificationManager>();
notificationManager.NotificationReceived += (sender, eventArgs) =>
{
  var evtData = (NotificationEventArgs)eventArgs;
  ShowNotification(evtData.Title, evtData.Message);
};

void ShowNotification(string title, string message)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        var msg = new Label()
        {
            Text = $"Notification Received:\nTitle: {title}\nMessage: {message}"
        };
        stacklayout.Children.Add(msg);
    });
}

我的主要活动

[Activity(Label = "THEHOCKEYLAB", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize, LaunchMode = LaunchMode.SingleTop )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);


         Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        

        LoadApplication(new App());
        CreateNotificationFromIntent(Intent);

        Window.SetStatusBarColor(Android.Graphics.Color.Black); //here

    }
    protected override void OnNewIntent(Intent intent)
    {
        CreateNotificationFromIntent(intent);
    }

    void CreateNotificationFromIntent(Intent intent)
    {
        if (intent?.Extras != null)
        {
            string title = intent.GetStringExtra(AndroidNotificationManager.TitleKey);
            string message = intent.GetStringExtra(AndroidNotificationManager.MessageKey);
            DependencyService.Get<INotificationManager>().ReceiveNotification(title, message);
        }
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

public interface INotificationManager
{
    event EventHandler NotificationReceived;
    void Initialize();
    void SendNotification(string title, string message, DateTime? notifyTime = null);
    void ReceiveNotification(string title, string message);

}

我收到以下错误。

System.NullReferenceException:“对象引用未设置为对象的实例。”

但是我看了看,在使用 Xamarin 表单版本 5.0.0.2083 时找不到任何东西

它在这条线上收到错误。

notificationManager.NotificationReceived += (sender, eventArgs) =>
{
    var evtData = (NotificationEventArgs)eventArgs;
    ShowNotification(evtData.Title, evtData.Message);
};
4

1 回答 1

1

没关系,我发现我的答案归功于这个 SO。像所有依赖项一样,它必须注册或始终为空。

Android 通知空指针异常。这需要在您的主要活动.cs 中。

 DependencyService.Register<INotificationManager, AndroidNotificationManager>();
于 2021-09-24T21:57:45.817 回答