0

I am new to Xamarin Android programming, I followed this Microsoft tutorial to send push notifications to Xamarin.Android apps using Notification Hubs

https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

Unfortunately the code does not compile and it gives the errors in this function

public void OnPushNotificationReceived(Context context, INotificationMessage message)
    {
        var intent = new Intent(this, typeof(MainActivity)); // error here, 'this' is not accepted
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); // error here, 'this' is not accepted

        var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID); // error here, 'this' is not accepted


        notificationBuilder.SetContentTitle(message.Title)
                    .SetSmallIcon(Resource.Drawable.ic_launcher)
                    .SetContentText(message.Body)
                    .SetAutoCancel(true)
                    .SetShowWhen(false)
                    .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);// error here, 'this' is not accepted 


        notificationManager.Notify(0, notificationBuilder.Build());
    }

The errors are

1>(path removed)\AzureListener .cs(11,33,11,37): error CS1503: Argument 1: cannot convert from 'AzureListener' to 'Android.Content.Context'

1>(path removed)\AzureListener .cs(13,55,13,59): error CS1503: Argument 1: cannot convert from 'AzureListener' to 'Android.Content.Context?'

1>(path removed)\AzureListener .cs(15,66,15,70): error CS1503: Argument 1: cannot convert from 'AzureListener' to 'Android.Content.Context'

1>(path removed)\AzureListener .cs(24,67,24,71): error CS1503: Argument 1: cannot convert from 'AzureListener' to 'Android.Content.Context'

I have tried to go through the tutorial three times but with same errors. Could anyone help to solve these errors? Thank you.

UPDATE: I am able to fix the errors by replacing 'this' with either 'context' or an object of MainActivity. After applying either one of them, I can run the app and receive a test notification from Azure (good news!) however the code is still not working (bad news!). It does not raise a notification, this bug was reported by another stackoverflow member here

Xamarin Forms using Azure Hubs. Push notifications not showing on android when app is running/active

Finally a message to Microsoft: please test your code before publish it, thanks!

peter
  • 11
  • 2

0 Answers0