I use the .NET Framework System.Windows.Forms.NotifyIcon class to display a tray icon and tray notifications. When I call ShowBalloonTip on its instance, first the previously displayed notification displays again, then it hides, and only after a while does the expected one is displayed. It seems that historical notifications are displayed first unless I remove them manually from the notification center.
What can I do programmatically to prevent showing the historical notifications again?
Here's how I initialize the notify icon:
notifyIcon.Icon = Properties.Resources.tray_icon;
notifyIcon.Visible = true;
notifyIcon.BalloonTipTitle = Language.TrayMessageTitle;
Here's how I show the toast:
notifyIcon.BalloonTipIcon = icon;
notifyIcon.BalloonTipText = message;
notifyIcon.ShowBalloonTip(0);
The icon is either ToolTipIcon.Error or ToolTipIcon.Info, and I don't show a toast with the same icon twice, so they always toggle. But this does not seem to matter.
The timeout param is 0, because it's not used since Windows Vista.
The toast is displayed as a result of an application state change, not as a result of a user action like clicking.
So I decided to stick with disposing the notify icon instance and recreating it every time I display a toast notification. I don't think it's fine, but I couldn't find a better solution.
I also dispose the notify icon on application shutdown to clean the notification center from previously displayed notifications. Otherwise, the last one displays first when another application displays a toast.
Related
I've built a C# Winforms App in which I've included toast notifications. The notifications work fine. However, when I change the ToastScenario to Reminder, the toast does not stay on screen like the Micorsoft documentation says it should (Reminder A reminder notification. This will be displayed pre-expanded and stay on the user's screen till dismissed.). I have no problem setting the duration to short or long, but I want this notification to stay visible until I interact with it. This app is just for me, so there is no danger of bothering the end user with a notification that persists until they click it. Here is the code that creates my notification:
new ToastContentBuilder()
.AddText(line1)
.AddText(line2)
.AddText(line3)
.AddArgument("link", link)
.AddInlineImage(new Uri(image))
.AddAudio(new Uri("C:\\Windows\\Media\\Alarm10.wav"))
.SetBackgroundActivation()
.SetToastScenario(ToastScenario.Reminder)
.Show();
I'm running the app on Windows 11 Pro. Amy thoughts on how to make the notification stay visible instead of just disappearing into the notifications bar?
I think Reminders require a button (an action that dismisses the toast). I added a button to my Toast and now it persists until the button is clicked. Like this:
new ToastContentBuilder()
.SetToastScenario(ToastScenario.Reminder)
.AddText(line1)
.AddText(line2)
.AddText(line3)
.AddButton(new ToastButton()
.SetContent("View")
.AddArgument("link", link)
.SetBackgroundActivation())
.AddInlineImage(new Uri(image))
.AddAudio(new Uri("C:\\Windows\\Media\\Alarm10.wav"))
.Show();
I have a Xamarin.Android app that shows lyrics from Genius and can detect music playing from the notifications.
In this app, the Main Activity contains a FrameLayout that is dynamically filled with a Welcome screen layout or a Song layout. The first is used when the user opens the app and no song has been detected, while the second appears when the user searches a song or opens the app through the notification sent when the app finds a song playing. Both the Welcome layout and the Song layout use ConstraintLayouts to display content.
This is how the layout looks without content:
Sometimes while loading the song, the layout size is not updated to fit the content inside the views. It has a loading wheel that is set to Gone when the lyrics finish loading.
Fully loaded, the screen should look like this:
However, it looks like this:
You can see that the broken layout is the same size and shape as it is on Android Studio.
The only thing I've been able to correlate with this bug is this LogCat message:
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy#2bd3336
This message always appears when this bug happens, but not the other way around. Sometimes it appears but the bug does not happen.
From what I've googled, this is related to the notification opening another instance of the MainActivity. My testing suggests this as well since the OnCreate method is called even when the app is already opened and the notification is touched. I have not been able to get this bug to occur without opening the app through the notification.
Here's the code that created the notification:
private void CreateNotification(string title, string artist)
{
Log.WriteLine(LogPriority.Verbose, "NLService", "CreateNotification: Creating notification");
MainActivity.fromNotification = true;
TaskStackBuilder stackBuilder = TaskStackBuilder.Create(Application.Context);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
stackBuilder.AddNextIntent(new Intent(Application.Context, typeof(MainActivity)));
PendingIntent resultIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetAutoCancel(true)
.SetContentTitle("SmartLyrics")
.SetContentText(artist + " - " + title)
.SetSmallIcon(Resource.Drawable.ic_stat_name)
.SetContentIntent(resultIntent)
.SetPriority(-1);
ntfManager = NotificationManagerCompat.From(Application.Context);
ntfManager.Notify(NOTIFICATION_ID, builder.Build());
Log.WriteLine(LogPriority.Info, "NLService", "CreateNotification (NLService): Notification made!");
}
I recorded my phone screen showing how it's behaving, first opening from the notification and then closing the app and opening the Song by searching from inside the app. Here's the LogCat for that session.
The whole project is open source, and the code is available here (the layout-testing branch is the one you should be looking at). Please spare me on how much this code is bad, I know about it and I'm slowly fixing stuff. This issue has been plaguing me for a long time and I want to get it fixed before working on other things.
Thanks in advance!
I try to show a balloon tip like the one in the screenshot:
First I created a notifyIcon
Then I added this code to the Form1_Load function:
private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.BalloonTipTitle = "Balloon Tip Title";
notifyIcon1.BalloonTipText = "Balloon Tip Text.";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
notifyIcon1.ShowBalloonTip(10000);
}
All I get is a little icon, and If I hover over it then i see the name of the notify icon.
I also tried this notifyIcon1.ShowBalloonTip(10000, "Text", "Title", ToolTipIcon.Warning); but then nothing happens.
I noticed in the function description of ShowBalloonTip, that the parameter "timeout" is deprecated from windows vista on, so what should I do instead?
PS: I run it on windows 10 64bit.
UPDATE 1/3:
I just created a fresh project, the balloon tip does not show either.
Maybe a setting in my OS blocks these messages?
UPDATE 2/3:
I downloaded the project from #pisi1001 but I get the same behaviour.
So I think it must be either a bug in windows 10, a wrong setting or group policy.
However like the next screenshots shows, the app is even allowed to show notifications:
UPDATE 3/3:
I noticed that you can even configure deeper if you double click on the setting, e.g. on "WindowsFormsApp1" from the last screenshot.
After I activated the setting in the red box in the last screenshot (which is basically saying "Show Notifications in the Info Center") I get at least notifications in the info center now:
This must be a Windows 10 Bug.
LAST UPDATE: A few weeks passed since I asked this question, now it seems to work and I am not sure why. Maybe Microsoft fixed it after I reported it to them.
In my Windows phone application, when the message box is displayed and if the phone is rotated, only the message box is getting rotated, whereas the UI behind it remains in the previous orientation.
It seems like the "Orientation changed" event of the page is not getting triggered when the message box is being displayed.
Although the UI behind the message box is not so prominent, I could find that the native messaging app of windows phone follows this behavior. I mean if the message box is displayed and the orientation is changed, the list behind the message box is also changed.
This image is of the native messaging app.
This image is from my application
You can notice the list behind the both the images.
Can I get some help in achieving this behavior ?
MessageBox.Show(string msg) is a synchronized method, meaning the method won't return until you dismiss the message box by pushing one of its buttons. So UI thread is blocked, and the OrientationChanged is only handled after the message box is dismissed since the event handler is running on UI thread, too.
MessageDialog.ShowAsync, which is only available on Windows Phone 8.1, should not have this limitation.
One alternative to MessageBox is the Popup control. You can lay it on top of your page, and set it IsOpen to true/false to show/hide it. Make it look good on both Landscape and Portrait orientations using ViewStates, just like other normal controls.
i am using this code under my form1_load
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(5000, "Welcome", "Hello " + User, ToolTipIcon.Info);
I even checked my registery and the value was 1. Why is the baloon not showing?
I do have a icon form my notify icon. and it is showing up. The Baloon is not though!
Looks like you forgot to set the Icon for it like this
notifyIcon1.Icon = SystemIcons.Exclamation;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(5000, "Welcome", "Hello " + User, ToolTipIcon.Info);
Also please read for more inormation on issues with NI http://www.csharp411.com/notifyiconshowballoontip-issues/
You may need to post the rest of the code that's in your form's load event, but here's a couple of suggestions:
Make sure the form's Load event is actually hooked up.
Make sure you've assigned an icon for the notify icon.
Also, note that the balloon tip isn't guaranteed to show. See the Remarks section on msdn's NotifyIcon.ShowBalloonTip Method article:
Remarks Minimum and maximum timeout values are enforced by the operating system and are typically 10 and 30 seconds,
respectively, however this can vary depending on the operating system.
Timeout values that are too large or too small are adjusted to the
appropriate minimum or maximum value. In addition, if the user does
not appear to be using the computer (no keyboard or mouse events are
occurring) then the system does not count this time towards the
timeout.
Only one balloon tip can display on the taskbar at a time. Attempting
to display a balloon tip when one is currently displayed on the
taskbar causes the timeout value to be ignored. The behavior is
slightly different depending on the operating system and whether the
balloon tip is from another, or the same, application. When the second
balloon tip is from another application, the first balloon tip will
display for the minimum timeout value before the second appears,
regardless of the value of timeout. In most cases, if the balloon tips
are from the same application, the first balloon tip immediately
closes when another call to the ShowBalloonTip method is made. In some
cases the second balloon will open on top of the first balloon.
The title text will display in a bold font near the top of the
balloon.
Here is some sample code for what #MetroSmurf has already mentioned. Note that this.InitializeComponent(); must be called before the NotifyIcon is shown (usually in the form constructor).
public Form1()
{
this.InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.Icon = new Icon(#"C:\SomePath\MyIcon.ico");
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(5000, "Welcome", "Hello " + User, ToolTipIcon.Info);
}
Also ensure that windows is configured to allow notifications. In Windows 7 right-click taskbar, click Properties, Customize... in the notification area, tick the Always show all icons and notifications on the taskbar option, click OK.