How update widget when notification coming ?

xNiuxxNiux USMember
edited January 2018 in Xamarin.Android

I'm trying to create a widget which indicate the number of missing call, number of new sms and other some notification.
I have a MainActivity.cs, an AppWidget.cs and a NotificationService.cs (for NotificationListenerService).

Currently, my widget is updating when I create one, or every 30mn. My ListenerService could catch notification posting or removing. But I can't update my widget from the ListenerService. could you help me please ?

My update function in ListenerService is called when it catch post or remove :

        void UpdateWidget()
        {
            RemoteViews view = new RemoteViews(PackageName, Resource.Layout.Widget);
            AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
            ComponentName thisWidget = new ComponentName(context.PackageName, Class.Name);
            appWidgetManager.UpdateAppWidget(thisWidget, view);
        }

And in my appwidget.cs, I have this function :

        public override void OnReceive(Context context, Intent intent)
        {
            switch (intent.Action)
            {
                default:
                    {
                        Log.Info("YotaWMNofifier", "** OnReceive received intent: {0}", intent.Action);
                    }
                    break;

                case AppWidgetManager.ActionAppwidgetUpdate:
                    {
                        Log.Info("YotaWMNofifier", "** OnReceive received intent: Update widget");
                        WidgetUpdater(context);
                    }
                    break;

                case AppWidgetManager.ActionAppwidgetDisabled:
                    {
                        Log.Info("YotaWMNofifier", "** OnReceive received intent: Delete last widget");
                    }
                    break;

                case AppWidgetManager.ActionAppwidgetEnabled:
                    {
                        Log.Info("YotaWMNofifier", "** OnReceive received intent: Create widget");
                        WidgetUpdater(context);
                    }
                    break;
            }
        }

I never have the Log.info from the OnReceive function, but I have it from OnNotificationRemoved and OnNotificationPosted of service

Sign In or Register to comment.