如何在Xamarin应用运行或手机未锁屏时接收推送通知
应用处于前台时系统默认不会自动弹出通知栏提示,你需要分别对Android和iOS端做额外配置即可实现全场景接收推送通知,具体实现方案如下:
Android端实现方案
Android平台应用前台运行时,推送的回调会触发FirebaseMessagingService的OnMessageReceived方法,你只需要在该方法内主动构建并弹出通知即可:
- 首先确保已安装
Xamarin.Firebase.MessagingNuGet包,且已正确配置Google服务相关文件 - 实现自定义消息接收服务,代码示例如下:
[Service(Exported = false)] [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] public class CustomFirebaseMessagingService : FirebaseMessagingService { public override void OnMessageReceived(RemoteMessage message) { base.OnMessageReceived(message); var notification = message.GetNotification(); if (notification != null) { ShowNotification(notification.Title, notification.Body); } } private void ShowNotification(string title, string content) { string channelId = "app_push_channel"; var intent = new Intent(this, typeof(MainActivity)); intent.AddFlags(ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot | PendingIntentFlags.Immutable); var notificationBuilder = new NotificationCompat.Builder(this, channelId) .SetSmallIcon(Resource.Mipmap.ic_launcher) .SetContentTitle(title) .SetContentText(content) .SetAutoCancel(true) .SetContentIntent(pendingIntent) .SetPriority(NotificationCompat.PriorityHigh); var notificationManager = NotificationManager.FromContext(this); // Android 8.0及以上需要创建通知渠道 if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { var channel = new NotificationChannel(channelId, "应用推送", NotificationImportance.High) { Description = "应用常规推送通知渠道" }; notificationManager.CreateNotificationChannel(channel); } // 生成随机ID避免通知覆盖 notificationManager.Notify(new Random().Next(1000, 9999), notificationBuilder.Build()); } }
- 部分国产Android系统需要额外引导用户开启通知权限、自启动权限,否则前台也可能无法正常弹出通知
iOS端实现方案
iOS平台需要实现UNUserNotificationCenterDelegate的WillPresentNotification方法,配置前台运行时的通知展示规则即可:
- 首先确保已在Entitlements.plist中开启Push Notifications功能,证书配置正确
- 在AppDelegate中注册通知并配置代理,代码示例如下:
public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new App()); // 申请通知权限 UNUserNotificationCenter.Current.RequestAuthorization( UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => { if (granted) { InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications); } }); // 设置通知代理 UNUserNotificationCenter.Current.Delegate = new CustomNotificationDelegate(); return base.FinishedLaunching(app, options); } // 自定义通知代理类 public class CustomNotificationDelegate : UNUserNotificationCenterDelegate { public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) { // 配置前台运行时也展示通知横幅、声音、角标 completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge); } public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) { // 此处处理通知点击逻辑 completionHandler(); } }
通用注意事项
- 推送分为通知消息和数据消息两种类型:通知消息系统默认仅在后台/锁屏时弹出,数据消息无论应用处于什么状态都会触发对应回调,需自行解析后弹出通知
- 可根据业务需求调整前台通知的展示样式,比如仅展示横幅、或仅播放提示音不弹出横幅
内容的提问来源于stack exchange,提问作者Zeeshan shaikh
相关产品推荐
相关产品推荐

