You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MAUI iOS端使用Plugin.LocalNotification显示本地通知?

MAUI iOS本地通知不显示问题排查与解决

1. 主动申请通知权限

iOS不会默认授予通知权限,必须主动向用户请求。在发送通知前执行以下代码:

using UserNotifications;

var center = UNUserNotificationCenter.Current;
var options = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
var authStatus = await center.RequestAuthorizationAsync(options);

if (!authStatus.Granted)
{
    // 权限未授予时,可提示用户前往系统设置开启
    return;
}

2. 配置Info.plist权限描述

在Platforms/iOS/Info.plist中添加权限说明,否则权限请求会被系统拒绝:

<key>NSUserNotificationUsageDescription</key>
<string>需要通知权限展示更新提醒</string>
<key>NSAlertUsageDescription</key>
<string>需要弹窗权限显示通知内容</string>

3. 修正通知资源路径与Build Action

iOS端资源路径规则与Android不同,需调整:

  • 将图片文件放在Platforms/iOS/Resources/Images目录下
  • 右键图片文件,设置Build Action为BundleResource
  • 代码中修改图片路径为"icon.png"(无需前缀)

4. 处理前台通知展示

iOS默认不会在App前台弹出通知,需添加代理配置:
在Platforms/iOS/AppDelegate.cs中补充以下代码:

public override void WillFinishLaunching(UIApplication application, NSDictionary launchOptions)
{
    base.WillFinishLaunching(application, launchOptions);
    UNUserNotificationCenter.Current.Delegate = new UserNotificationDelegate();
}

public class UserNotificationDelegate : UNUserNotificationCenterDelegate
{
    public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
    {
        // 允许前台展示通知的样式:弹窗+声音+角标
        completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
    }
}

5. 调试环境注意事项

  • 模拟器:检查模拟器设置→通知→你的App,确认已开启“允许通知”,且未开启专注模式
  • 真机:确保设备通知设置中已允许App发送通知,同时关闭可能屏蔽通知的专注模式

调整后的完整示例代码

// 先请求通知权限
var center = UNUserNotificationCenter.Current;
var options = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
var authStatus = await center.RequestAuthorizationAsync(options);

if (!authStatus.Granted)
{
    return;
}

// 发送通知
var request = new NotificationRequest
{ 
    Image = new NotificationImage { ResourceName = "icon.png", FilePath = "icon.png" },
    NotificationId = 1000,
    Title =  "Changed",
    Subtitle = "Tap to see..",
    Description = $"updated",
    BadgeNumber = 50,

    Schedule = new NotificationRequestSchedule
    {
        NotifyTime = DateTime.Now.AddSeconds(1),
    }
};
await LocalNotificationCenter.Current.Show(request);

内容的提问来源于stack exchange,提问作者okd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 05:53:21