Some devices do not receive push notifications

Billy12ShovelsBilly12Shovels USUniversity ✭✭✭
edited July 2016 in Xamarin.Android

Hello,
I've noticed in my testing, that some devices do not receive push notifications when the app is closed.
Is this a device to device issue or an issue with my code?
Here is my Receiver:

public class MyBroadcastReceiver : GcmBroadcastReceiverBase<PushHandlerService>
{
    public static string[] SENDER_IDS = new string[] { App.SenderID };

    public const string TAG = "MyBroadcastReceiver-GCM";
}

and my service:

[Service] // Must use the service tag
public class PushHandlerService : GcmServiceBase
{
    public static string RegistrationID { get; private set; }
    private NotificationHub Hub { get; set; }

    public PushHandlerService()
        : base(App.SenderID)
    {
        Log.Info(MyBroadcastReceiver.TAG, "PushHandlerService() constructor");
    }

    protected override void OnRegistered(Context context, string registrationId)
    {
        FindNDMobile.Helpers.Settings.AppToken = registrationId;

        //Log.Verbose(MyBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
        //RegistrationID = registrationId;

        ////createNotification("PushHandlerService-GCM Registered...",
        //                    //"The device has been Registered!");

        ////DeviceInstallation installation = new DeviceInstallation()
        ////{
        ////    platform = "gcm",
        ////    pushChannel = registrationId,
        ////    installationId = Guid.NewGuid().ToString(),
        ////};

        ////GlobalMethods.RegisterForNotifications(App.FullSharedString, App.NotificationHubName, installation);

        //Hub = new NotificationHub(App.NotificationHubName, App.ListenConnectionString,
        //                            context);
        //try
        //{
        //    Hub.UnregisterAll(registrationId);
        //}
        //catch (Exception ex)
        //{
        //    Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        //}

        ////var tags = new List<string>() { "falcons" }; // create tags if you want
        //List<string> tags = new List<string>();

        //if (FindNDMobile.Helpers.Settings.resident)
        //{
        //    List<string> list = new List<string>();
        //    list.Add(FindNDMobile.Helpers.Settings.Home);

        //    tags = list;
        //}
        //else
        //{
        //    List<string> list = JsonConvert.DeserializeObject<List<string>>(FindNDMobile.Helpers.Settings.jsonCityList);

        //    tags = list;
        //}

        //try
        //{
        //    var hubRegistration = Hub.Register(registrationId, tags.ToArray());
        //    FindNDMobile.Helpers.Settings.registered = true;
        //}
        //catch (Exception ex)
        //{
        //    FindNDMobile.Helpers.Settings.registered = false;
        //    Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        //}
    }

    protected override void OnMessage(Context context, Intent intent)
    {
        Log.Info(MyBroadcastReceiver.TAG, "GCM Message Received!");

        var msg = new StringBuilder();

        if (intent != null && intent.Extras != null)
        {
            foreach (var key in intent.Extras.KeySet())
                msg.AppendLine(key + "=" + intent.Extras.Get(key).ToString());
        }

        NotifyDroid note = new NotifyDroid();

        string messageText = intent.Extras.GetString("message");
        if (!string.IsNullOrEmpty(messageText))
        {
            //createNotification("New hub message!", messageText);
            note.CreateNotification("Find ND", messageText);
        }
        else
        {
            //createNotification("Unknown message details", msg.ToString());
            note.CreateNotification("Message error", msg.ToString());
        }
    }

    protected override void OnUnRegistered(Context context, string registrationId)
    {
        Log.Verbose(MyBroadcastReceiver.TAG, "GCM Unregistered: " + registrationId);

        //createNotification("GCM Unregistered...", "The device has been unregistered!");
    }

    protected override bool OnRecoverableError(Context context, string errorId)
    {
        Log.Warn(MyBroadcastReceiver.TAG, "Recoverable Error: " + errorId);

        return base.OnRecoverableError(context, errorId);
    }

    protected override void OnError(Context context, string errorId)
    {
        Log.Error(MyBroadcastReceiver.TAG, "GCM Error: " + errorId);
    }

    protected void dialogNotify(string title, string message)
    {
        MainActivity.instance.RunOnUiThread(() =>
        {
            AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.instance);
            AlertDialog alert = dlg.Create();
            alert.SetTitle(title);
            alert.SetButton("Ok", delegate
            {
                alert.Dismiss();
            });
            alert.SetMessage(message);
            alert.Show();
        });
    }
}

Thank you!

Sign In or Register to comment.