本地推送通知在Xamarin iOS中不起作用

本地推送通知在Xamarin iOS中不起作用,xamarin,xamarin.forms,push-notification,xamarin.ios,xamarin-studio,Xamarin,Xamarin.forms,Push Notification,Xamarin.ios,Xamarin Studio,我正在使用Xamarin.forms并为iOS实现本地推送通知。当我通过visual studio调试应用程序时,它正在成功运行,即使应用程序最小化,应用程序也可以接收通知。但是,在不通过visual studio进行调试的情况下直接运行应用程序时,该应用程序无法显示通知。请在这方面指导我 然后,我也尝试将应用程序发布到应用程序商店,但经历了同样的情况,应用程序无法接收通知,甚至在前台模式下也无法接收 我已经在Info.plist中选择了后台模式下的“后台获取”属性。 我在FinishedLau

我正在使用Xamarin.forms并为iOS实现本地推送通知。当我通过visual studio调试应用程序时,它正在成功运行,即使应用程序最小化,应用程序也可以接收通知。但是,在不通过visual studio进行调试的情况下直接运行应用程序时,该应用程序无法显示通知。请在这方面指导我

然后,我也尝试将应用程序发布到应用程序商店,但经历了同样的情况,应用程序无法接收通知,甚至在前台模式下也无法接收

我已经在Info.plist中选择了后台模式下的“后台获取”属性。 我在FinishedLaunching方法中还添加了以下行

UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
代码的整个实现如下所示

   public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        try
        {
            UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

            try
            {                    

                if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
                {
                    UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert |
                        UNAuthorizationOptions.Sound |
                        UNAuthorizationOptions.Sound,
                        (granted, error) =>
                        {
                            if (granted)
                            {
                                InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);                                    
                            }
                        });
                }
                else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                    new NSSet());

                    UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
                    UIApplication.SharedApplication.RegisterForRemoteNotifications();
                }
                else
                {
                    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
                }

                bool IsRegistered = UIApplication.SharedApplication.IsRegisteredForRemoteNotifications;
            }
            catch (Exception ex)
            {
                UIAlertView avAlert = new UIAlertView("FinishedLaunching Push Notification Exception", ex.Message, null, "OK", null);
                avAlert.Show();
            }

            UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

            LoadApplication(new MessengerClient.App());

        }
        catch (Exception ex)
        {
            NativeHelper.SendUnhandledException(ex, NativeHelper.iOS + ": FinishedLaunching");
        }
        return base.FinishedLaunching(app, options);
    }

    public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
    {  
        /// reset our badge
        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;

        /// Cancel/clear all Local notifications fronm the tray.
        UIApplication.SharedApplication.CancelLocalNotification(notification);

        /// Cancel/clear all notifications fronm the tray.
        UIApplication.SharedApplication.CancelAllLocalNotifications();
    }
显示通知的代码如下所示

UILocalNotification notification = new UILocalNotification();
        notification.FireDate = NSDate.FromTimeIntervalSinceNow(1);
        notification.AlertAction = title;
        notification.AlertBody = content;
        notification.AlertTitle = title;
        notification.SoundName = UILocalNotification.DefaultSoundName;
        notification.ApplicationIconBadgeNumber = 1;            
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);

我知道这是一个重复的问题,但是,我尝试了所有的解决方法,但没有对我起作用。

你可以检查一下这个

iOS应用程序以几乎完全相同的方式处理远程和本地通知。当应用程序运行时,将调用
AppDelegate
类上的
ReceivedLocalNotification
方法或
ReceivedRemoteNotification
方法,并将通知信息作为参数传递

应用程序可以用不同的方式处理通知。例如,应用程序可能只是显示一个提醒用户某些事件的警报。或者,该通知可能用于向用户显示进程已完成的警报,例如将文件同步到服务器

以下代码显示如何处理本地通知、显示警报以及将徽章编号重置为零:

public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
    // show an alert
    UIAlertController okayAlertController = UIAlertController.Create(notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
    okayAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

    Window.RootViewController.PresentViewController(okayAlertController, true, null);

    // reset our badge
    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
此外,您还可以下载查看。无论是在我的本地站点(iOS 13.3)的调试还是发布版模式下,它都可以工作

其效果是:


只需在AppDelegate DidFinishLaunching中添加此代码,通知将在后台开始工作。BackgroundTask-showNotification可能会在某个时候被取消,对于我来说,每当我的仪表板被加载时,我猜是因为它有多个API调用。因此,将其添加到您的DienterBackground委托中,并使用不同的taskID,以启动新的后台任务。这对我来说很好

nint taskID = yourTaskID;
taskID = application.BeginBackgroundTask("showNotification", expirationHandler: ()=> {
     UIApplication.SharedApplication.EndBackgroundTask(taskID);
});

我已经使用了您建议的链接,并相应地设置了代码。我只想在应用程序最小化时设置本地通知。当我通过visual studio调试应用程序时,它工作正常,与上图中的操作相同(但我使用的是真实设备),但当我从系统中拔下设备并直接运行应用程序时,当应用程序处于最小化代码中时,它无法接收通知。当应用程序最小化或手机锁定时,我希望收到通知。让我举个例子,我的应用程序具有聊天功能,当应用程序在后台运行且有人发送消息时,我希望使用本地通知通知用户(而不是远程通知-因为我的应用程序已在最小化模式下运行)@VivekPatel-Okey,如果在聊天功能中,我认为您最好使用自定义服务器向设备发送远程通知。这将保证无论设备是后台还是锁定,应用程序都会收到通知。Jiang,谢谢你的建议,我已经知道远程推送通知,但我的本地通知代码在Android平台上运行良好,所以我想在我的iOS上实现相同的本地通知,但不幸的是,在iOS的情况下,它不起作用。“你知道我上面解释的问题吗?”VivekPatel我用物理设备试过,但无法重现。它起作用了,现在我不知道这个原因:(