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

寻求支持定时触发及跳转指定页面的Xamarin本地通知NuGet插件

Xamarin定时本地通知全方案(后台触发+指定页面跳转)

嘿,刚接触Xamarin就搞定定时通知需求,其实你找的那两个插件完全能覆盖你的要求,我帮你把具体步骤拆解开,确保应用没运行也能触发,点击通知还能跳转到指定页面:

一、先搞定插件安装

先在你的Xamarin.Forms项目,还有对应的Android、iOS子项目里装上对应的NuGet包,选最新的稳定版就行,避免踩旧版本的坑。

二、Android端核心配置(重点!处理后台触发和跳转)

Android这边因为有Doze模式和后台限制,得仔细配置:

1. 配置AndroidManifest.xml

先把必要的权限和组件加上:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application ...>
  <!-- 处理通知触发的广播接收器 -->
  <receiver android:name="com.plugin.notifications.NotificationReceiver" />
  <!-- 你的目标Activity,记得设置launchMode避免重复打开 -->
  <activity android:name=".NotificationActivity" android:launchMode="singleTop" />
</application>

2. 设置通知点击跳转逻辑

创建通知的时候,要把跳转的PendingIntent绑上去,这样点通知才会跳去你要的页面:

// 构建跳转到NotificationActivity的Intent
var intent = new Intent(Android.App.Application.Context, typeof(NotificationActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
// 注意加上Immutable标记,适配Android 12+
var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);

// 组装通知的时候把这个PendingIntent传进去
var notification = new Notification.Builder(Android.App.Application.Context)
    .SetContentTitle("你的定时提醒")
    .SetContentText("该做你设定的事情啦~")
    .SetSmallIcon(Resource.Drawable.ic_notification_icon) // 记得替换成你的图标
    .SetContentIntent(pendingIntent)
    .Build();

3. 后台定时触发(应用没运行也能生效)

要用AlarmManager来调度,而且得用SetExactAndAllowWhileIdle才能绕过Doze模式:

// 创建触发通知的广播Intent
var alarmIntent = new Intent(Android.App.Application.Context, typeof(NotificationReceiver));
var pendingAlarmIntent = PendingIntent.GetBroadcast(Android.App.Application.Context, 1, alarmIntent, PendingIntentFlags.Immutable);

// 比如设置10分钟后触发
var alarmManager = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
var triggerTime = SystemClock.ElapsedRealtime() + 10 * 60 * 1000;
alarmManager.SetExactAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, triggerTime, pendingAlarmIntent);

然后在NotificationReceiver的OnReceive方法里,把刚才写的通知显示逻辑放进去就行。

三、iOS端配置

iOS这边相对简单,用UNUserNotificationCenter就能搞定:

1. 配置Info.plist

先加权限和后台模式:

<key>NSUserNotificationUsageDescription</key>
<string>需要给你发送定时提醒哦</string>
<key>UIBackgroundModes</key>
<array>
  <string>remote-notification</string>
</array>

2. 定时通知+跳转逻辑

先请求权限,再创建定时通知,最后处理点击跳转:

// 先申请通知权限
var center = UNUserNotificationCenter.Current;
var authStatus = await center.RequestAuthorizationAsync(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound);
if (!authStatus) return;

// 创建通知内容
var content = new UNMutableNotificationContent();
content.Title = "定时提醒";
content.Body = "别忘记你设定的任务~";
content.Sound = UNNotificationSound.Default;

// 设置10分钟后触发
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(600, false);

// 提交通知请求
var request = UNNotificationRequest.FromIdentifier("TimedReminder", content, trigger);
await center.AddNotificationRequestAsync(request);

然后在AppDelegate里处理点击跳转:

public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    // 跳转到你的NotificationViewController
    var targetVC = new NotificationViewController();
    UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(targetVC, true, null);
    completionHandler();
}

四、踩坑提示

  • Android应用后台不触发:一定要用SetExactAndAllowWhileIdle,别用普通的Set方法,不然Doze模式下会被拦截。
  • 点击通知不跳转:检查PendingIntent的Flags有没有加对,Android 12+必须加Immutable;iOS要确保在AppDelegate里正确处理了通知响应。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:23:30