首页 > 解决方案 > 根据当前登录的用户过滤 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));
}

有没有办法在应用程序处于后台时拦截和过滤推送通知,并在应用程序未运行时阻止它们(因为还没有用户登录)?

标签: androidxamarin.formspush-notificationvisual-studio-app-center

解决方案


要在发送推送通知时过滤用户,最好设置用户 ID

AppCenter.SetUserId("your-user-id");

然后在appcenter.ms上转到Push> Send notificaiton。填写必填字段。然后在第二步中选择User list而不是All registered devices。然后输入用户 ID,以逗号分隔。


推荐阅读