当应用程序在Xamarin iOS中处于后台时,本地通知不显示

时间:2020-07-02 12:12:08

标签: xamarin.ios notifications localnotification unusernotificationcenter ios-background-mode

我已经在Xamarin.IOS应用程序中实现了聊天功能,因为无法从服务器端发送远程通知,我正在使用本地通知向用户发出新的传入消息的警报。在模拟器中一切正常,我可以看到通知既显示在前台也显示在后台模式。

但是当我在设备上运行应用程序时,通知仅在前台模式下有效。当应用程序在后台运行时,不会显示通知。

研究此问题后,我发现当应用程序在调试模式下运行时它们可以正常工作(即从VisualStudio启动并且电缆仍连接)。但是,一旦我断开电缆,它们就不会显示了。

我发现有几则帖子https://stackoverflow.com/a/35029847暗示此行可以解决此问题,但是由于处理程序不能再为null了,因此它将不起作用。 application.BeginBackgroundTask("showNotification", expirationHandler: null);

所以我在我的应用程序委托中实现了以下代码,但仍无法正常工作

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

我也尝试了后台获取,并在AppDelegate的行下方添加了它,但是没有用。 application.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

计划本地通知代码

var content = new UNMutableNotificationContent();
            content.Body = body;
            content.Sound = UNNotificationSound.Default;
            content.UserInfo = dict;
            content.Badge = IOSAppConstant.LocalNotificationCount + 1;

            var request = UNNotificationRequest.FromIdentifier(chatID, content, null);

            // schedule it
            UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) =>
            {
                if (error != null)
                {
                    Console.WriteLine($"Error: {error.LocalizedDescription ?? ""}");
                }
                else
                {
                    Console.WriteLine("Scheduled...");
                }
            });

//注册通知

UNUserNotificationCenter.Current.RequestAuthorization
                (UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
                                                                    (granted, error) =>
                                                                    {
                                                                        if (granted)
                                                                            InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
                                                                    });

//分配代表

this._userNotificationCenterDelegate = new UserNotificationCenterDelegate();
UNUserNotificationCenter.Current.Delegate = this._userNotificationCenterDelegate;

//通知代表

public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
     public override void WillPresentNotification(UNUserNotificationCenter center, 
     UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
     {
          completionHandler(UNNotificationPresentationOptions.Alert);
     }

     public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, 
     UNNotificationResponse response, Action completionHandler)
     {
          completionHandler();
     }
}

我也尝试过在我的聊天控制器上从BeginBackgroundTask调用schedule Notification函数,但是它不起作用

问题类似于Local Push notification not working in Xamarin iOS,但没有可用的解决方案。

1 个答案:

答案 0 :(得分:0)

您提到您无法使用此问题 https://stackoverflow.com/a/3502984 中的答案,因为您无法为 BeginBackgroundTask 的 expireHandler 提供 null。 为了解决这个问题,您可以提供任意操作。结果是当发布的应用程序在后台时本地通知应该可以工作

public void NotifAction()
        {
            // This exists because expirationHandler can't be null
        }

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {

            global::Xamarin.Forms.Forms.Init();
            LoadApplication(new App());
            UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

            Action testActon = NotifAction;

            UIApplication.SharedApplication.BeginBackgroundTask("showNotification", testActon);
            return base.FinishedLaunching(app, options);
        }