Have implemented NotificationListenerService in my app as below, if I use sbn.PackageName (commented below) app crashes. It appears to be a .Net MAUI bug, because while developing the same in Java it(sbn.getPackageName()) seem to work fine . In order to see which Package the notification is targeted to, I have to identify this info from the received notification, appreciate any help in this regard. EDIT : Further trouble shooting realized that this "PackageName" works fine in Emulator, but when deploying in real device (tested in 2 devices Redmi Note 5 pro and 10i) app crashes upon opening itself.
[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 below line is uncommented, APP will CRASH
// if(sbn.PackageName == "com.xyzapp"){ //Do Something }
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);
}
}
Below is first few lines of error report
backtrace:
#00 pc 000000000002216c /system/lib64/libc.so (abort+116)
#01 pc 0000000000039f1c /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::mono_log_handler(char const*, char const*, char const*, int, void*)+144)
#02 pc 00000000002da9a8 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonosgen-2.0.so
#03 pc 00000000002daad4 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonosgen-2.0.so
#04 pc 00000000002dab18 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonosgen-2.0.so
#05 pc 00000000000df8a8 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonosgen-2.0.so
#06 pc 0000000000113c0c /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonosgen-2.0.so (mono_method_get_unmanaged_callers_only_ftnptr+60)
#07 pc 0000000000026108 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::init_android_runtime(_JNIEnv*, _jclass*, _jobject*)+1320)
#08 pc 0000000000027bac /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::create_and_initialize_domain(_JNIEnv*, _jclass*, xamarin::android::jstring_array_wrapper&, xamarin::android::jstring_array_wrapper&, _jobjectArray*, xamarin::android::jstring_array_wrapper&, _jobject*, bool, bool, bool)+160)
#09 pc 000000000002b0f4 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::Java_mono_android_Runtime_initInternal(_JNIEnv*, _jclass*, _jstring*, _jobjectArray*, _jstring*, _jobjectArray*, int, _jobject*, _jobjectArray*, int, unsigned char, unsigned char)+4116)
#10 pc 000000000002e5e4 /data/app/com.companyname.wmoni-AciNshhqc0_S-X8oLMzwAw==/lib/arm64/libmonodroid.so (Java_mono_android_Runtime_initInternal+88)
When you implemented NotificationListenerService, please check if the service has been enabled at first.
And I used the following code to go to the Notification settings to check if the app has been granted by the permission.
In addition, starting from the Android 12.0, the component with intent filter need to declare the exported attribute. So you can declare it for the service:
After this, the NotificationListenerService worked for me.
The result image: