JesusHF
JesusHF

Reputation: 21

Xamarin Forms Push Notifications using Azure Messaging Hub not working in release mode

I added a functionality to my Xamarin Forms App allowing it to receive Notifications from Azure. Everything works fine in debug mode, but in release mode, the App crashes when I receive a notification.

I got a logger working with Adb to see the exception. The code I am using is the following:

public void OnPushNotificationReceived(Context context, INotificationMessage message)
    {
        try
        {
            var msgData = message.Data;

            if (msgData.ContainsKey("EventType"))
            {
                try
                {
                    if (AllowStoreNotification(msgData["VirtualStoreId"], msgData["EventType"]))
                    {
                        SendNotification(message.Title, message.Body);
                    }
                }
                catch (KeyNotFoundException ex)
                {
                    new TrackEvent("Could not deliver notification. Parameter error.")
                        .AddParameter("exception", ex.Message.ToString())
                        .Send();
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error("Notification", ex.Message);
        }

        void SendNotification(string title, string body)
        {
            var notification = new NotificationCompat.Builder(context, "PushNotifications")
                           .SetContentTitle(title)
                           .SetContentText(body)
                           .SetSmallIcon(Resource.Drawable.logo_pink_circle)
                           .SetAutoCancel(true)
                           .SetDefaults((int)NotificationDefaults.All)
                           .SetPriority((int)NotificationPriority.High)
                           .Build();

            if (_notificationManager is null)
            {
                NotificationManagerCompat.From(context).Notify(0, notification);
            }
            else
            {
                _notificationManager.Notify(0, notification);
            }
        }
    }

The _notificationManager is null for version Build.VERSION.SdkInt >= BuildVersionCodes.O.

The exception from de ADB log adb logcat Notification:E *:S:

Notification: no non-static method "Lcom/microsoft/windowsazure/messaging/notificationhubs/BasicNotificationMessage;.getData()Ljava/util/Map;"

Anyone knows how to fix this?

Upvotes: 1

Views: 310

Answers (2)

JesusHF
JesusHF

Reputation: 21

I figured out how to make it work using the skip linking assembly. AndroidProject

Upvotes: 1

Isaac Rebolledo
Isaac Rebolledo

Reputation: 36

The issue is the certificate you uploaded to azure, it's just for development, not for production You have a few options:

  • Change the certificate once you move to production
  • 2 clouds, one for development and another for production.
  • Or just compile your app as production every time you want to test notifications

Upvotes: 0

Related Questions