How do I get my App to receive even closed or blocked notification

Asked

Viewed 721 times

0

If I lock my Cell screen and leave the App open, I get Notifications, but if I close the App I can’t receive any more. The problem that when it started working, was getting in all situations and suddenly it didn’t work anymore. I’m trying to understand what was done, because suddenly there’s no way to stop work. Sending:

public static async Task<IFCMResponse> SenderMessage()
        {
            FCMClient client = new FCMClient("XXXXXXXXXXX"); //as derived from https://console.firebase.google.com/project/
            var message = new Message()
            {
                To = "XXXXXXXXXX", //topic example /topics/all
                Notification = new AndroidNotification()
                {
                    Body = "Solicitação de Novo Desconto",
                    Title = "Desconto",
                }
            };
            var result = await client.SendMessageAsync(message);
            return result;
        }

Class that processes notifications:

[assembly: Permission(Name = "com.global.autorizador.br.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "com.global.autorizador.br.permission.C2D_MESSAGE")]
[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]
[assembly: UsesPermission(Name = "android.permission.INTERNET")]
[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
//GET_ACCOUNTS is only needed for android versions 4.0.3 and below
[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]

namespace Gcm.Client
{
    [BroadcastReceiver(Permission = Gcm.Client.Constants.PERMISSION_GCM_INTENTS)]
    [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "com.global.autorizador.br" })]
    [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "com.global.autorizador.br" })]
    [IntentFilter(new string[] { Gcm.Client.Constants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "com.global.autorizador.br" })]
    public class PushHandlerBroadcastReceiver : GcmBroadcastReceiverBase<GcmService>
    {
        public static string[] SENDER_IDS = new string[] {Constants.SENDER_ID };
    }
    [Service]
    public class GcmService : GcmServiceBase
    {
        public static string RegistrationID { get; private set; }

        public GcmService()
            : base(PushHandlerBroadcastReceiver.SENDER_IDS) { }

        protected override void OnRegistered(Context context, string registrationId)
        {
            Log.Verbose("PushHandlerBroadcastReceiver", "GCM Registered: " + registrationId);
            RegistrationID = registrationId;
        }

        protected override void OnMessage(Context context, Intent intent)
        {
            Log.Info("PushHandlerBroadcastReceiver", "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());
            }

            //Store the message
            var prefs = GetSharedPreferences(context.PackageName, FileCreationMode.Private);
            var edit = prefs.Edit();
            edit.PutString("last_msg", msg.ToString());
            edit.Commit();

            string message = intent.Extras.GetString("message");
            if (!string.IsNullOrEmpty(message))
            {
                createNotification("New todo item!", "Todo item: " + message);
                return;
            }

            string msg2 = intent.Extras.GetString("msg");
            if (!string.IsNullOrEmpty(msg2))
            {
                createNotification("New hub message!", msg2);
                return;
            }

            createNotification("Solicitação de novo desconto", msg.ToString());
        }

        void createNotification(string title, string desc)
        {
            //Create notification
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            //Create an intent to show ui
            var uiIntent = new Intent(this, typeof(MainActivity));

            //Use Notification Builder
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

            //Create the notification
            //we use the pending intent, passing our ui intent over which will get called
            //when the notification is tapped.
            var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
                                      .SetSmallIcon(Resource.Drawable.icon)
                                      .SetTicker(title)
                                      .SetContentTitle(title)
                                      .SetContentText(desc)

                    //Set the notification sound
                    .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))

                    //Auto cancel will remove the notification once the user touches it
                    .SetAutoCancel(true).Build();

            //Show the notification
            notificationManager.Notify(1, notification);
        }

        protected override void OnUnRegistered(Context context, string registrationId)
        {
            Log.Error("PushHandlerBroadcastReceiver", "Unregistered RegisterationId : " + registrationId);
        }

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

    }
  • try to implement a foreground service to receive your notifications.

  • @Tobiasmesquita, I read the last link and I don’t know if that’s what I’m looking for. What I want is for example I’m on the street or having lunch and suddenly I get the notification that there is a new discount to be granted. Usually in these cases, the cell is all closed. That’s what I’m talking about.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.