Ad

Filter AppCenter Push Notifications Depending On The Current Logged In User

Using AppCenter I am able to send push notification to all my devices with my Xamarin Forms (android only) app.

Since my devices, are going to be shared, I can't do the filter of the notifications on the AppCenter side based on the devices IDs.

I need to make the filter based on the current logged in user to my application. For this with the push notification I also send the WorkerID, which serves as a filter.

While I'm able to do this filter when the app is in foreground, its not working when the app is in background or not running.(normal behaviour since the push event is in App Start)

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));
}

Is there a way to intercept and filter the push notifications when the app is in background and block them when the app is not running (since no user is yet logged in) ?

Ad

Answer

To filter users when sending push notifications it's preferable to set user id:

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

Then on the appcenter.ms go to Push >Send notificaiton. Fill in the required fields. And then in the second step select User list instead of All registered devices. Then enter user ids, separated by commas.

Ad
source: stackoverflow.com
Ad