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

问题描述 投票:0回答:1

我正在使用Xamarin.forms,并为iOS实现了本地推送通知。当我通过Visual Studio调试应用程序时,即使该应用程序已最小化,该应用程序也能够接收通知,它已成功运行。但是,在直接运行应用程序而未通过Visual Studio进行调试的情况下,该应用程序将无法显示通知。请为此指导我。

然后,我也尝试通过将应用发布到应用商店来尝试,但是经历了同样的事情,即使在前台模式下,应用也无法接收到通知。

我已经在Info.plist的Background Modes下选择了“ Background fetch”属性。我还在我的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);

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

xamarin xamarin.forms push-notification xamarin.ios xamarin-studio
1个回答
0
投票

您可以对此Notifications in Xamarin.iOS进行支票。

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

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

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

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;
}

另外,您可以下载a sample进行检查。无论在我的本地站点(iOS 13.3)中的DebugRelease模型中,它都可以工作。

enter image description here

效果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.