android - 根据当前登录的用户过滤 AppCenter 推送通知
问题描述
使用 AppCenter,我可以使用我的 Xamarin Forms(仅限 Android)应用程序向我的所有设备发送推送通知。
由于我的设备将被共享,因此我无法根据设备 ID 在 AppCenter 端过滤通知。
我需要根据当前登录用户对我的应用程序进行过滤。为此,我还通过推送通知发送 WorkerID,它用作过滤器。
虽然我可以在应用程序处于前台时执行此过滤器,但当应用程序处于后台或未运行时它不起作用。(正常行为,因为推送事件在应用程序启动中)
protected override void OnStart()
{
// This should come before AppCenter.Start() is called
// Avoid duplicate event registration:
if (!AppCenter.Configured)
{
Push.PushNotificationReceived += (sender, e) =>
{
var title = e.Title;
var message = e.Message;
// If app is in background title and message are null
// App in foreground
if (!string.IsNullOrEmpty(title))
{
foreach (string key in e.CustomData.Keys)
{
if (e.CustomData[key] == Settings.WorkerID)
{
Current.MainPage.DisplayAlert(title, message, "OK");
}
}
}
};
}
// Handle when your app starts
AppCenter.Start("android=xxxxxxxxxxxxxxxxxx", typeof(Push));
}
有没有办法在应用程序处于后台时拦截和过滤推送通知,并在应用程序未运行时阻止它们(因为还没有用户登录)?
解决方案
要在发送推送通知时过滤用户,最好设置用户 ID:
AppCenter.SetUserId("your-user-id");
然后在appcenter.ms上转到Push
> Send notificaiton
。填写必填字段。然后在第二步中选择User list
而不是All registered devices
。然后输入用户 ID,以逗号分隔。
推荐阅读
- ios - 如何使用 Swift 4 将 Json 数据放入 UITableView?
- c - 使用 execvp 声明范围错误
- javascript - 有没有办法将函数调用或语句延迟给定的毫秒数?
- php - 如何在桌子上的每个循环上求和
- python - Keras - 我的模型总是返回精度“0”和损失“nan”
- c# - 无法使用 gRPC 编译 C# 项目
- jquery - 获取 JSON 响应中每个对象的最后一个值
- android - 错误:找不到参数 [arm64-v8a] 的方法 armeabi-v7a()
- c# - Visual Studio App Center 中的应用程序更新通知程序
- angularjs - Angularjs http拦截器导致没有路由发生