Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

[Xamarin.Android] NotificationListenerService Issue

CarbongripCarbongrip USMember ✭✭
edited April 2017 in Xamarin.Android

Hello everyone,

I'm having an issue with my NotificationListenerService, it doesn't seem to be connecting to listener. My service is currently only calling OnCreate for some reason as I do receive "service running!".

My Code:

NotificationListener.cs

using System;
using System.Collections.Generic;

using Android.App;
using Android.Service.Notification;
using Android.Util;

namespace NotificationPushAndroid
{
    [Service(Label = "NotificationListener", Permission = "android.permission.BIND_NOTIFICATION_LISTENER_SERVICE")]
    class NotificationListener : NotificationListenerService
    {
        static readonly string TAG = "X:" + typeof(NotificationListener).Name;

        public override void OnCreate()
        {
            base.OnCreate();
            Log.Debug(TAG,"service running!");
        }

        public override void OnListenerConnected()
        {
            base.OnListenerConnected();

            Log.Debug(TAG, "Listener Connected!");
            List<string> l = new List<string>();

            foreach (StatusBarNotification sbn in GetActiveNotifications())
            {
                l.Add(sbn.Notification.TickerText.ToString());
                Log.Debug(TAG, sbn.Notification.TickerText.ToString());
            }

        }

        public override void OnNotificationPosted(StatusBarNotification sbn)
        {
            Console.WriteLine("Notification Posted!");
            Log.Debug(TAG,"ID :" + sbn.Id + "t" + sbn.Notification.TickerText + "t" + sbn.PackageName);
        }
    }
}

MainActivity.cs

    using System;

    using Android;
    using Android.App;
    using Android.Content;
    using Android.Widget;
    using Android.OS;
    using Android.Content.PM;
    using Android.Support.Design.Widget;
    using Android.Support.V4.App;

    namespace NotificationPushAndroid
    {
        [Activity(Label = "NotificationPushAndroid", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat")]
        public class MainActivity : Activity
        {
            protected override void OnCreate(Bundle bundle)
            {
                base.OnCreate(bundle);

                // Set our view from the "main" layout resource
                SetContentView (Resource.Layout.Main);

                Button button1 = FindViewById<Button>(Resource.Id.button1);
                button1.Click += button1_Clicked;

                StartService(new Intent(this, typeof(NotificationListener)));

            }
        }
    }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>










Notification Access Enabled

Answers

  • CarbongripCarbongrip USMember ✭✭

    Manifest didn't post good here it is again...

    <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="NotificationPushAndroid.NotificationPushAndroid" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
            <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
            <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
            <application android:label="NotificationPushAndroid">
            <service android:name=".NotificationListener"
                     android:label="NotificationPushAndroid"
                     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
              <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
              </intent-filter>
            </service>
          </application>
        </manifest>
    
Sign In or Register to comment.