Search code examples
c#androidxamarinibeaconproximity

Try displaying notification with xamarin


I'm following this guide for implementing an app that uses proximity beacon:

https://github.com/Estimote/Xamarin-Bindings

I downloaded the ExampleApp folder from github and installed Estimote.Android.Proximity. It work fine.

But this is my problem:

//ExampleApps/Example.Android.Proximity/MainActivity.cs

class MyEnterHandler : Java.Lang.Object, Kotlin.Jvm.Functions.IFunction1
    {
        public Java.Lang.Object Invoke(Java.Lang.Object p0)
        {
            IProximityZoneContext context = (IProximityZoneContext)p0;

            Log.Debug("app", $"MyEnterHandler, context = {context}");

            return null;
        }
    }

Instead of Log.Debug I want to create my Notification.. How can I do it?

I tryed to create notification with this script:

 if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            // Android 8.0 and up require a channel for the notifications
            var channel = new NotificationChannel(channelId, "Bluetooth activity", NotificationImportance.Low);
            var notificationManager = this.GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.CreateNotificationChannel(channel);
        }
        var notification = new NotificationCompat.Builder(this, channelId)
                .SetSmallIcon(global::Android.Resource.Drawable.IcDialogInfo)
                .SetContentTitle("Proximity")
                .SetContentText("Proximity demo is scanning for beacons")
                .Build();

But I have the problem with this.getSystemService (I haven't Context in "MyEnterHandler class")


Solution

    • create Notification channel
    void CreateNotificationChannel()
            {
                if (Build.VERSION.SdkInt < BuildVersionCodes.O)
                {
                    return;
                }
    
            var channel = new NotificationChannel(CHANNEL_ID, Resources.GetString(Resource.String.app_name), NotificationImportance.Default)
            {
                Description = ""
            };
    
            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);
        }
    
    • use notificationManager to send notification
    var pendingIntent = PendingIntent.GetActivity(this, MainActivity.NOTIFICATION_ID, intent, PendingIntentFlags.OneShot);
    
                var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                                          .SetSmallIcon(Resource.Drawable.outline_message_24)
                                          .SetContentTitle(Resources.GetString(Resource.String.app_name))
                                          .SetContentText(messageBody)
                                          .SetAutoCancel(true)
                                          .SetContentIntent(pendingIntent);
    
                var notificationManager = NotificationManagerCompat.From(this);
                notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());