Search code examples
androidxamarinpush-notificationgoogle-cloud-messagingintercom

Unable to register for push notifications on Android in Xamarin together with IntercomIO


I added the IntercomIO SDK to our Xamarin.Forms app through a couple binding libraries this week and I'm currently trying to get the Push Notifications to work but soon after I call PushHandlerService.Register(this) in the MainActivity the app crashes saying that it can't find the class com.google.android.gms.gcm.GcmReceiver which isn't even being caught by the try catch block around this call.

Here is the method inside the MainActivity which is responsible for setting up the push notifications on Android.

    public void registerForPushNotifications()
    {
        try
        {
            GcmClient.CheckDevice(this);
            GcmClient.CheckManifest(this);

            //Register the app for push notifications.
            PushHandlerService.Initialize(this);

            //if (!GcmClient.IsRegistered(this))//Temporarily force the app to register for push notifications
            {
                System.Diagnostics.Debug.WriteLine("Registering");

                // Register for GCM
                PushHandlerService.Register(this);
            }

            LocalBroadcastManager lbc = LocalBroadcastManager.GetInstance(this);
            PushActionReceiver rec = new PushActionReceiver(this);
            lbc.RegisterReceiver(rec, new IntentFilter("pushaction"));
        }
        catch (Java.Net.MalformedURLException)
        {
            var e = new Exception("There was an error creating the Mobile Service. Verify the URL");
            System.Diagnostics.Debug.Fail(String.Format(@"Exception at {0}: {1}", this.GetType().Name, e.Message));
            Insights.Report(e);
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.Fail(String.Format(@"Exception at {0}: {1}", this.GetType().Name, e.Message));
            if (e.GetType() != typeof(TaskCanceledException))
                Insights.Report(e);
        }
    }

And in the Manifest I added the receiver definition for Intercom

<receiver android:name="io.intercom.android.sdk.gcm.GcmBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"></receiver>

The issue doesn't happen when I don't call PushHandlerService.Register(this) but then obviously I can't receive any push notifications anymore (including the ones from our own system)

What's going on here? I have the libraries and dependancies setup properly but it doesn't seem to be able to find the GcmReceiver class..


Solution

  • Apparently updating to the latest SDK's in the SDK Manager solved this crash. I am still not receiving any push notifications but I'm guessing this is due to a different issue. At least the app doesn't crash anymore when trying to register for the push notifications.