How can I implement push notifications in a MonoGame iOS/Andoid project?

I want to use push notifications in my iOS and Android project and I need a working tutorial and code. I have tried this tutorial for Azure push notifications today but the code isn’t working. I get an error message when I run my iOS project.
Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

System.Exception has been thrown
Could not initialize an instance of the type ‘WindowsAzure.Messaging.SBNotificationHub’: the native ‘initWithConnectionString:notificationHubPath:’ method returned nil.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

I have not tested the implementation for Android because it isn’t working on iOS and I need a solution that works on iOS and Android.

What is the easiest way to implement push notifications in a MonoGame iOS/Android game?

Well, why are you not looking into Native PN instead of using third party external systems? is this your approach to disconnect them or do you need server push?

My point is, do you want the notification from the game or a server? either way, I think you still need to utilise native push instead of third party since I think third party won’t get background activity access, possibly… still a new area for me especially on those platforms…

I want to send sometimes short messages to the people, for example if new levels are available. Just as a reminder so that they play again my game. The game has no online features(except in-app purchases and ads) and I don’t use a server.
I don’t know how to use native PN. How can I use that? What is the best choice for my project? Third party or native PN?

I know you mean you are not hosting a server but excuse me whilst I giggle a little…

Take a look there, and follow the internal discussion links

It works now on iOS. ListenConnectionString and NotificationHubName had not the correct string.
Android Tutorial
I tried this tutorial for Android but it doesn’t work for me and I don’t know what could be wrong because I get no error message. I have tried to send a push notification in Azure portal to my Android device. Azure portal indicates that the notification was succesfully sent but the notification doesn’t show up in my application.
What could be wrong? Is this tutorial outdated or am I doing something wrong?

Are you listening or enabling ports locally?

I didn’t enable ports locally. I just enabled “Internet” in AndroidManifest.xml.
I found out that my application gets the notification from the Azure portal because the breakpoints got executed and .SetContentText(messageBody) has the same string that I have sent from Azure portal to my Android device. But the push notification doesn’t show up in my application.

Is something wrong with void SendNotification(string messageBody)?
Am I missing something(some code?) or why is the notification not showing up in the application?

MyFirebaseMessagingService.cs:

[Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";

        public override void OnMessageReceived(RemoteMessage message)
        {
            Log.Debug(TAG, "From: " + message.From);
            if (message.GetNotification() != null)
            {
                //These is how most messages will be received
                Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
                SendNotification(message.GetNotification().Body);
            }
            else
            {
                //Only used for debugging payloads sent from the Azure portal
                SendNotification(message.Data.Values.First());

            }

        }

        void SendNotification(string messageBody)
        {
            var intent = new Intent(this, typeof(Activity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Notification.Builder(this)
                        .SetContentTitle("FCM Message")
                        .SetSmallIcon(Resource.Drawable.ic_launcher)
                        .SetContentText(messageBody)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

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

MyFirebaseIIDService.cs:

[Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    public class MyFirebaseIIDService : FirebaseInstanceIdService
    {
        const string TAG = "MyFirebaseIIDService";
        NotificationHub hub;

        public override void OnTokenRefresh()
        {
            var refreshedToken = FirebaseInstanceId.Instance.Token;
            Log.Debug(TAG, "FCM token: " + refreshedToken);
            SendRegistrationToServer(refreshedToken);
        }

        void SendRegistrationToServer(string token)
        {
            // Register with Notification Hubs
            hub = new NotificationHub(Constants.NotificationHubName,
                                      Constants.ListenConnectionString, this);
                                      
            var tags = new List<string>() { };
            var regID = hub.Register(token, tags.ToArray()).RegistrationId;

            Log.Debug(TAG, $"Successful registration of ID {regID}");
        }
    }

Activity1.cs:

 [Activity(Label = "Pushtestandro.Android"
        , MainLauncher = true
        , Icon = "@drawable/icon"
        , Theme = "@style/Theme.Splash"
        , AlwaysRetainTaskState = true
        , LaunchMode = LaunchMode.SingleInstance
        , ScreenOrientation = ScreenOrientation.FullUser
        , ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize)]
    public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity
    {
        public const string TAG = "MainActivity";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            if (Intent.Extras != null)
            {
                foreach (var key in Intent.Extras.KeySet())
                {
                    if (key != null)
                    {
                        var value = Intent.Extras.GetString(key);
                        Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
                    }
                }
            }
            var g = new Game1();
            SetContentView((View)g.Services.GetService(typeof(View)));
            g.Run();
        }
    }

Isn’t there an official forum for this as I don’t think this is really a MonoGame related issue…

MonoGame Sample
I found this MonoGame sample for Azure push notifications but it doesn’t work. I created a new solution and added the code and the two nuget packages Microsoft.Azure.EventHubs and WindowsAzure.Storage to my Android and iOS project but I just get these error messages:
on iOS:

/Users/…/Desktop/MonogameNotifications/MonogameiOS/MTOUCH: Error MT2001: Could not link assemblies. Reason: Error while processing references of ‘MonogameiOS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ (MT2001) (MonogameiOS.iOS)

on Android:

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2,2): Error XA2002: Can not resolve reference: Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, referenced by Microsoft.Azure.Services.AppAuthentication. Please add a NuGet package or assembly reference for Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, or remove the reference to Microsoft.Azure.Services.AppAuthentication. (XA2002) (MonogameAndroid.Android)

Why is it not working? Which nuget packages should I add to my projects in order to use the EventHub samples for iOS and Android?

:confused:I don’t really know…