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

Asked

Viewed 247 times

0

This is the Oncreate from my Mainactivity. Note that I have a badge with a value of 10 for testing. Now I need to make sure that by submitting 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 handles 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 have the Badge that is in Mainactivity manage only the badge according to the received message(s) and for that I need to set something in Mainactivity. How do I do it?

Use XF

  • Still GCM? Not updated to FCM not Xamarim?

  • @Jeffersonquesado, good morning. So I was trying with FCM but I wasn’t getting it. So I took this code from a guy and put it in and I was able to send PN. Since that period, I haven’t tried anything else with PN, because mine is working. The guy told me, that even FCM uses under the covers the GCM, I do not understand. I have another App to develop and this I will test the FCM yes. I just need to finish this one. As for the post, I created a Static Property in Mainactivity and Seto it by the Gcmservice class. If true, generates the badge thus: Autorizador.Android.MainActivity.HasMessage = true.

  • I already did. I created a service class, in Application reality and in it I call the badge and when I want to reset, step Count(0) and this works.

No answers

Browser other questions tagged

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