When sending a notification, how do I display the badge icon?

0

This is the OnCreate of my MainActivity. See that I have a badge with a value of 10 for testing. Now I need to do what, when sending a Notification, I can increment the badge icon with the amount of Notification being received by my App. The message is received by another class (GcmService) and put the two methods that receive and create the message.

protected override void OnCreate(Bundle bundle)
        {
            instance = this;
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            Badge badge = new Badge(this);
            badge.count(10);
            LoadApplication(new App());
        }

Here are the two methods of the Gcm class that treats the message.

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;
            }

            //CrossBadge.Current.SetBadge(++contador, "Novo desconto");
            createNotification("Solicitação de novo desconto", "Novo desconto na fila");//msg.ToString());


        }

        void createNotification(string title, string desc)
        {
            contador++;
            //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();



            //string count = contador.ToString();
            //CrossBadge.Current.SetBadge(++contador, "Nova solicitação de desconto");

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

I just need to make the Badge that is in MainActivity only generate the badge according to the message (s) received and for this I need to set something in MainActivity. How do I do this?

Use XF

    
asked by anonymous 16.10.2017 / 01:28

0 answers