question

PaWeLL-6215 avatar image
0 Votes"
PaWeLL-6215 asked PaWeLL-6215 edited

How to read foreing push notifications with Xamarin App

Hello.

Unfortunately couldn't find information about this question so I'll just ask.
Is it even possible to read foreing push notifications within Xamarin app. For example notifications from Google Pay?
And how to achieve that?
Thank you.

dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you want to achieve the firebase notification in xamarin.android? If so, please check this path: https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm, If not, could you give me some details about the foreing push notifications.

0 Votes 0 ·

I have Xamarin App that reads financial SMS. I'd like to make it read push notifications from google pay and banks' apps as well.

0 Votes 0 ·

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered PaWeLL-6215 edited

Hello,​

Welcome to our Microsoft Q&A platform!

I have Xamarin App that reads financial SMS. I'd like to make it read push notifications from google pay and banks' apps as well.

For Android, we can use NotificationListenerService to achieve it like following code. For iOS, it cannot be achieved.

1.Create a service to extend the NotificationListenerService. If the notification is coming, you can get the content from notification from OnNotificationPosted method.

[Service(Label = "ServiceName", Permission = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE")]
    [IntentFilter(new[] { "android.service.notification.NotificationListenerService" })]
    public class NLService : NotificationListenerService
    {
        public override void OnCreate()
        {
            base.OnCreate();
            Log.Info("start running", "Servico Criado");
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
        }
        public override IBinder OnBind(Intent intent)
        {
            return base.OnBind(intent);
        }
        public override bool OnUnbind(Intent intent)
        {
            return base.OnUnbind(intent);
        }
        public override void OnNotificationPosted(StatusBarNotification sbn)
        {
          var  notification= sbn.Notification;
            Bundle extras = notification.Extras;
            if (extras != null)
            {
                // Get the title from notification
                string title = extras.GetString(Notification.ExtraText, "");

                // Get the content from notification
                string content = extras.GetString(Notification.ExtraText, "");
                Console.WriteLine("============" + content + "======================");
            }

           
           
            base.OnNotificationPosted(sbn);
        }

        public override void OnNotificationRemoved(StatusBarNotification sbn)
        {
            base.OnNotificationRemoved(sbn);
        }
    }


2.Then use it with Intent to open system page, then enable it for your applicaion.

Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
 StartActivity(intent);


127011-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (47.3 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, Leon Lu. I will try to adapt this solution. Can it be run as foreground service?

0 Votes 0 ·

Tried but no luck.
Foreground service is started but OnNotificationPosted not fired when I send messages to test device. :(

0 Votes 0 ·

I did not test it in the foreground service, I run this code in Activity, it worked. It is a normal service (above code), android OS will close it when you application running in background after several minutes, because the Background Execution Limits in Android 8.0


0 Votes 0 ·

So, it turns out that I can't rely on Activity and should consider only Foreground Service as relatively guaranteed method to catch all notifications. Or I am wrong?

0 Votes 0 ·