Forum Xamarin Xamarin.Android

How can I receive push notifications even when app is "closed"?

rumurillorumurillo USMember
edited May 2016 in Xamarin.Android

Hi there.

I know this might've been asked multiple times. I have actually read/implemented/tried lots of solutions, but none of them have worked.

I have implemented a BroadcastReceiver to handle push notifications in my Xamarin.Android app from Windows Azure Notification Hub. I receive notifications when my app is in Foreground and Background. However, when my app is "closed" (removed from recents) I do not receive any notification.

Here's my BroadcastReceiver:

[assembly: Permission (Name = "@[email protected]_MESSAGE")]
[assembly: UsesPermission (Name = "@[email protected]_MESSAGE")]
[assembly: UsesPermission (Name = "")]
[assembly: UsesPermission (Name = "android.permission.INTERNET")]
[assembly: UsesPermission (Name = "android.permission.WAKE_LOCK")]

namespace appnamespace.Droid
    [BroadcastReceiver (Permission = Gcm.Client.Constants.PERMISSION_GCM_INTENTS)]
    [IntentFilter (new []{ Intent.ActionBootCompleted })]
    [IntentFilter (new [] { Gcm.Client.Constants.INTENT_FROM_GCM_MESSAGE },
        Categories = new [] { "@[email protected]" })]
    [IntentFilter (new [] { Gcm.Client.Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
        Categories = new [] { "@[email protected]" })]
    [IntentFilter (new [] { Gcm.Client.Constants.INTENT_FROM_GCM_LIBRARY_RETRY },
        Categories = new [] { "@[email protected]" })]

    public class BroadcastReceiver : GcmBroadcastReceiverBase<PushHandlerService>
        public static string[] SENDERIDS = { Constants.SenderID };

And here's my GcmServiceBase

namespace appnamespace.Droid
    public class PushHandlerService : GcmServiceBase

        public PushHandlerService () : base (Constants.SenderID)

        #region implemented abstract members of GcmServiceBase

        protected override void OnMessage (Context context, Intent intent)
            /*var message = new StringBuilder ();
            if (intent != null && intent.Extras != null) {
                foreach (var key in intent.Extras.KeySet ()) {
                    message.AppendLine (key + "=" + intent.Extras.Get (key));

            string messageText = intent.Extras.GetString ("message");
            if (!string.IsNullOrEmpty (messageText)) {
                NotificationRouter.ReceivedNotification (messageText);

        protected override void OnError (Context context, string errorId)

        protected override void OnUnRegistered (Context context, string registrationId)

        protected override void OnRegistered (Context context, string registrationId)
            RegisterAppWithServer (registrationId);


    private void RegisterAppWithServer(string registrationId)
    //TODO: Register the app with my backend service

        public static void RegisterWithGCM (Context _context)
            try {
                GcmClient.CheckDevice (_context);
                GcmClient.CheckManifest (_context);

                GcmClient.Register (_context, Constants.SenderID);
            } catch (Exception ex) {
                Insights.Report (ex);
                Debug.WriteLine ("Error @ Registering Device.");

How can I BroadcastReceiver that receives push notifications even when app is "closed"?

Thanks in advance for your help.


    edited May 2016

    Set following permissions in manifest :smile:

    <?xml version="1.0" encoding="utf-8"?>

  • rumurillorumurillo USMember


    I followed the tutorial and it works... Sort of. I receives notifications while app is 1) in Foreground and 2) in Background. But when I swipe the app off the recent screen, and then send a remote push notification... A "Unfortunately, has stopped" error message is displayed.

    I am unable to debug this issue because app is "closed".

    Any idea how can I solve this?

    Thanks in advance


    @rumurillo , "Unfortunately, has stopped" message is because your application is crashing somewhere, Debug your application and find out where it is crashing !

    Push Notifications works both in foreground and in background because you start a service (GcmListenerService) that always listen to GCM, There might be some sort of error in OnMessageReceived.

  • MaxMengMaxMeng NZMember ✭✭✭

    Error can be found in logs (logcat), or you could catch uncaught exceptions.

  • lucascclucascc BRMember

    Can anybody help with this?
    I've tried a lot of thing and none of them works.


  • DanielKrzyczkowskiDanielKrzyczkowski PLUniversity ✭✭

    Please switch from debug to release mode - then you will receive notifications even if your app is removed from "recent apps".

  • fabio.spillerfabio.spiller ITMember ✭✭
    edited March 2017

    @DanielKrzyczkowski said:
    Please switch from debug to release mode - then you will receive notifications even if your app is removed from "recent apps".

    (revived thread) Actually not :neutral:

    It is strange that this happens only with Azure notifications. Firebase push notifications work wonders even with app swiped away from recents.

  • DanielKrzyczkowskiDanielKrzyczkowski PLUniversity ✭✭

    Please check my sample on GitHub:
    I was able to configure each platform (Xamarin.Android, Xamarin,iOS and UWP):

    Notifications are received even if you close app swipe it from recents.

  • SmeSme USMember ✭✭✭

    Being able to receive notifications while the app is swiped away from recents also depends on the phone. For example, I notice on my Samsung Galaxy S5, I can still receive notifications after swiping away the app. But on my Huawei G6, after swiping away from recents, I no longer receive notifications. This may have something to do with Huawei's battery saving "feature".

  • garropulosgarropulos USMember

    I receive push notification when app is running but not when i close it

    I use the example of @DanielKrzyczkowski
    I have a Hawei P8 Light. How can receive the push notification when The app is killed\closed?
    Please can you help me?

  • DanielKrzyczkowskiDanielKrzyczkowski PLUniversity ✭✭

    Do you test your app in "release" mode? in "debug" mode there will be no notification displayed.

  • NamAnhNamAnh Member

    @DanielKrzyczkowski said:
    Do you test your app in "release" mode? in "debug" mode there will be no notification displayed.

    It's worked for me. Thks

  • SagarPanwalaSagarPanwala USMember ✭✭✭

    @DanielKrzyczkowski : I hope no one can argue that there is some issue, because I ran my app on release mode and for sure there is some bug.
    If I turned off Battery Optimisation for the app, I'm receiving the notification. I'm seeing fb, whatsapp and there are many local apps which are not popular are still receiving the notification, so it's clear there should be something to do with code or within app.
    @garropulos : I totally agree with you. You can check Battery Optimisation setting.

  • Android:

    Manifest file:

    <uses-permission android:name="android.permission.WAKE_LOCK" />
        android:protectionLevel="signature" />
    <uses-permission android:name="" />
    <uses-permission android:name="" />
    <uses-permission android:name="android.permission.VIBRATE" />

    And once deploy application using release mode then try

Sign In or Register to comment.