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

如何在MAUI Android应用中为通知添加操作选项?

在MAUI中为本地通知添加操作按钮

1. 基础依赖与命名空间

首先确保项目引用Microsoft.Maui.ApplicationModel.Notifications命名空间,MAUI的通知API已封装了跨平台的操作按钮实现,无需直接调用Android原生的Notification.Action。

2. 创建通知操作按钮

定义一个方法生成操作按钮实例,指定操作ID、显示标题和触发类型:

using Microsoft.Maui.ApplicationModel.Notifications;

private NotificationAction CreateGoForwardAction()
{
    return new NotificationAction(
        actionId: "go_forward_action", // 唯一标识操作,用于后续判断
        title: "Go Forward", // 按钮显示文本
        type: NotificationActionType.Foreground); // 点击后唤醒APP到前台执行逻辑
}
  • NotificationActionType.Foreground:点击按钮后将APP带到前台处理逻辑
  • NotificationActionType.Background:在后台静默执行逻辑(需额外配置平台权限,如Android后台服务、iOS后台模式)

3. 构建并发送带操作的通知

将操作按钮添加到通知请求中,通过NotificationCenter发送:

public async Task SendNotificationWithAction()
{
    var notificationRequest = new NotificationRequest
    {
        Identifier = "sample_notification", // 通知唯一ID
        Title = "Sample Notification",
        Description = "This is a notification with action button",
        Actions = new List<NotificationAction> { CreateGoForwardAction() } // 添加操作按钮
    };

    await NotificationCenter.Current.ScheduleNotificationAsync(notificationRequest);
}

4. 处理操作按钮的点击事件

有两种常用方式处理操作回调:

方式一:订阅NotificationActionTapped事件

在App.xaml.cs的构造函数中注册事件,全局监听操作点击:

public App()
{
    InitializeComponent();
    MainPage = new AppShell();

    // 注册通知操作点击事件
    NotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;
}

private void OnNotificationActionTapped(object sender, NotificationActionEventArgs e)
{
    if (e.ActionId == "go_forward_action")
    {
        // 执行Go Forward对应的逻辑,比如导航到指定页面
        MainThread.BeginInvokeOnMainThread(() =>
        {
            if (MainPage is Shell shell)
            {
                shell.GoToAsync("//ForwardPage");
            }
        });
    }
}

方式二:重写OnAppLinkRequestReceived方法

MAUI会将通知操作点击转化为App Link请求,可在App.xaml.cs中重写该方法处理:

protected override void OnAppLinkRequestReceived(Uri uri)
{
    base.OnAppLinkRequestReceived(uri);

    if (uri.AbsolutePath == "/notification-action")
    {
        var actionId = uri.QueryParameters["action"];
        if (actionId == "go_forward_action")
        {
            // 执行对应逻辑
            MainThread.BeginInvokeOnMainThread(() =>
            {
                // 示例:导航到目标页面
                if (MainPage is Shell shell)
                {
                    shell.GoToAsync("//ForwardPage");
                }
            });
        }
    }
}

5. 平台权限配置

  • Android(13+):在Platform/Android/AndroidManifest.xml中添加通知权限:
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
  • iOS:在Platform/iOS/Info.plist中添加通知权限说明和后台模式(若需后台操作):
    <key>NSUserNotificationUsageDescription</key>
    <string>需要发送通知提醒您</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>remote-notification</string>
    </array>
    

内容的提问来源于stack exchange,提问作者Álvaro García

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 01:05:19