繁体   English   中英

Xamarin Forms iOS本地通知未显示

[英]Xamarin Forms iOS Local Notifications not displaying

我正在Xamarin.Forms中构建应用程序,并且无法使本地通知显示在iOS中。 我遵循了https://developer.xamarin.com/guides/ios/platform_features/user-notifications/enhanced-user-notifications/中的指南,但通知仍未显示。 我在AppDelegate的FinishedLaunching方法中添加了以下内容:

    UNUserNotificationCenter.Current.RequestAuthorization (UNAuthorizationOptions.Alert, (approved, err) => {});

在我想要显示通知的页面中:

using UserNotifications;


var content = new UNMutableNotificationContent ();
content.Title = "Notification Title";
content.Subtitle = "Notification Subtitle";
content.Body = "Test Notification.";
content.Badge = 1;

var trigger =  UNTimeIntervalNotificationTrigger.CreateTrigger (5, false);

var requestID = "sampleRequest";
var request = UNNotificationRequest.FromIdentifier (requestID, content, 
trigger);

UNUserNotificationCenter.Current.AddNotificationRequest (request, (err) => {
if (err != null) {
    // Do something with error...
}
});

我所有的nuGet软件包都是最新的,并且我尝试在iOS iPhone模拟器和物理iPad设备上进行测试。

我在这里缺少什么吗?

啊! 这是我两天以来一直误解的重要信息。 Xamarin示例在应用程序上具有一个按钮,该按钮会触发本地通知。

这会被忽略,因为当您按下按钮时,应用程序(上面有按钮)位于前台!

实际上,如果您的应用程序位于前台,则默认情况下,iOS系统将使通知静音。

解决方案:

有两种解决方案供您选择:

  1. 当您触发延迟的通知时,通过单击“主页”按钮进入后台。 然后将显示通知。

  2. 实现委托userNotificationCenter:willPresentNotification:withCompletionHandler:,当前台有通知到达您的应用程序时,它将被调用。 例如,下面是用于在应用程序处于前台状态时启用通知的代码段:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM