Xamarin.ios 当应用程序在Xamarin iOS中处于后台时,本地通知不显示
我已经在我的Xamarin.IOS应用程序中实现了聊天功能,因为无法从服务器端发送远程通知,所以我使用本地通知提醒用户新收到的消息。在模拟器中一切正常,我可以看到通知显示在前台和后台模式中 但当我在设备上运行应用程序时,通知仅在前台模式下工作。当应用程序位于后台时,不会显示通知 在调查此问题后,我发现当应用程序在调试模式下运行时(即从VisualStudio启动,电缆仍连接)它们工作正常。但一旦我断开电缆,它们就不再显示了 我发现一些帖子建议这行代码可以解决这个问题,但由于处理程序不能再为null,所以它将无法工作。Xamarin.ios 当应用程序在Xamarin iOS中处于后台时,本地通知不显示,xamarin.ios,notifications,localnotification,unusernotificationcenter,ios-background-mode,Xamarin.ios,Notifications,Localnotification,Unusernotificationcenter,Ios Background Mode,我已经在我的Xamarin.IOS应用程序中实现了聊天功能,因为无法从服务器端发送远程通知,所以我使用本地通知提醒用户新收到的消息。在模拟器中一切正常,我可以看到通知显示在前台和后台模式中 但当我在设备上运行应用程序时,通知仅在前台模式下工作。当应用程序位于后台时,不会显示通知 在调查此问题后,我发现当应用程序在调试模式下运行时(即从VisualStudio启动,电缆仍连接)它们工作正常。但一旦我断开电缆,它们就不再显示了 我发现一些帖子建议这行代码可以解决这个问题,但由于处理程序不能再为nul
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();
}
}
//通知代表
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();
}
}
公共类UserNotificationCenterDelegate:UnuseNotificationCenterDelegate
{
公共覆盖无效将显示通知(UNUserNotificationCenter,
未通知通知通知,操作完成处理程序)
{
completionHandler(UNNotificationPresentationOptions.Alert);
}
public override void direceiveNotificationResponse(UNUserNotificationCenter,
UNNotificationResponse响应,Action completionHandler)
{
completionHandler();
}
}
我还尝试在聊天控制器上从BeginBackgroundTask调用日程通知功能,但它不起作用
nint taskID = 111;
taskID = application.BeginBackgroundTask("showNotification", expirationHandler: ()=> {
UIApplication.SharedApplication.EndBackgroundTask(taskID);
});
此问题与类似,但没有可用的解决方案。您提到无法使用此问题的答案,因为无法为BeginBackgroundTask的expirationHandler提供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);
}
可以从服务发送通知。我知道推送通知是如何工作的,我已经在我的应用程序中实现了它。但是我使用的框架没有推送通知功能。回到这个问题上,我看到这个解决方案被建议得最多。application.BeginBackgroundTask(“showNotification”,expirationHandler:null);你知道如何在IOS13中实现这一点吗?你可以使用它。@semeinc我也面临同样的问题。在模拟器上或调试时工作正常。但是,在生活模式下,实际设备上没有。你找到解决这个问题的办法了吗?