c# - 条件处理 Firebase 推送通知
问题描述
我有一个支持 Android 和 iPhone 的 Xamarin.Forms 应用程序。该应用使用 Firebase for Android 和 Azure Notification Hub for iOS 发送和接收推送通知。如果应用程序从发件人的手机发送通知,组中的多个人会收到并可以处理它。但这不是我们需要的。我只希望第一个接收者处理通知,其他人应该忽略它。有没有好的方法来实现它?
这是我在 Android 上处理推送通知的代码:
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
try
{
base.OnMessageReceived(message);
string messageBody = message.GetNotification()?.Body;
VideoCallMessage videoCallMessage = JsonConvert.DeserializeObject<VideoCallMessage>(messageBody);
if (App.UserContext.IsEmployee)
{
// convert the incoming message to a local notification
SendLocalNotification(messageBody);
}
}
catch (Exception ex)
{
Log.Debug("FirebaseService.OnMessageReceived()", $"Exception in OnMessageReceived(). ErrorMessage: {ex.Message}, Stack Trace: {ex.StackTrace}");
Microsoft.AppCenter.Crashes.Crashes.TrackError(ex); // Report the exception to App Center
}
}
对于 Delegate.cs 中的 iOS:
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
completionHandler();
NSDictionary userInfo = response.Notification.Request.Content.UserInfo;
ProcessNotification(userInfo);
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
NSDictionary userInfo = notification.Request.Content.UserInfo;
ProcessNotification(userInfo);
}
解决方案
推荐阅读
- java - 解释为什么这个二叉树遍历算法有 O(NlogN) 时间复杂度?
- mongodb - 我试图在 $unwind 之后返回嵌套文档的完整列表
- c# - ASP.NET Core:BindRequired 和适当的错误
- php - Symfony,使用 ajax 将 html 表单提交给控制器
- php - PHP:fwrite大字符串
- unit-testing - 开玩笑地编写单元测试时会出现特定错误
- android - 是否可以将自定义类放入 Bundle 中?
- git - 为什么重新定位到已重新定位的分支会引入合并冲突并且似乎会删除我当前的更改?
- node.js - 可以在没有数据库连接的情况下使用 sequelize
- reactjs - ESLint - 如何禁用反应/禁止外国道具类型?