How to use a Bitmap image from Resources in Windows Toast Notifications - c#

I am trying to build an unpackaged WPF app with Windows 10 Toast Notifications. I would like to make an AppLogoOverride to get a resource from the Resources class of my application. I have tried to do this so far:
public void ShowToast() {
var toast = new ToastContentBuilder()
.AddText("Foo!")
.AddText("Bar.")
.AddAppLogoOverride(new Uri("pack://application:,,,/Resources/foobar.png"), ToastGenericAppLogoCrop.Circle)
.SetToastScenario(ToastScenario).Reminder);
ToastNoticiationManagerCompat.CreateToastNotifier().Show(toast);
}
When doing so, the notification just says the generic "New Notification" text. So I tried commenting out the part that says AddAppLogoOverride, and it started to work. So I am not sure how to get the image from Resources.
I would also like to mention that there is no way for me to use something like ms-appdata:// or others, I am not sure how to go about doing this, and I really don't want to write the file locally to the disk.

I have not succeeded to include an image into a toast using pack://application:,,, syntax so far. I guess this syntax is not supported by toast notification because it is a part of WinRT and the syntax of UWP to refer resources is different from that of WPF.
The workaround is, you might dislike it, to copy the image in local (in the installation folder of the app or somewhere else) and specify its absolute path. See How can I make a notification in a C# Windows app with a custom image and onclick function?

Related

Toast notifications missing application icon when launched from service

Question:
Why does my unpackaged C# application show its icon when I launch it but not when my Windows service launches it? How can I make my app's toasts always show the app's icon?
Details:
I have a C++ Windows Service that launches a C# Win32 application for toast functionality, since toasts cannot be launched directly from a service to a user. It is an absolute requirement that the service launches the toast app. To my frustration, however, the app's icon (i.e. the icon shown on the .exe in Explorer) refuses to show only when launched by my service. Here is an example of what I see when my service launches the app (Note the three squares. This is the Windows 10 default icon):
When I manually launch the app (i.e. click it), this is what I see instead:
The only difference between the above two screenshots is the launch method. The most succinct way I can describe my issue is that I want the launch method (launched from a service) that yields the first screenshot to yield the second screenshot instead.
I can provide the code snippet I used to generate these toasts, although I doubt its usefulness for finding a solution:
var notifier = ToastNotificationManagerCompat.CreateToastNotifier();
var xml = new Windows.Data.Xml.Dom.XmlDocument();
xml.LoadXml("<toast><visual><binding template=\"ToastGeneric\"><text>Foo</text<text>Bar</text></binding></visual></toast>");
var notif = new Windows.UI.Notifications.ToastNotification(xml);
notifier.Show(notif);
The most useful code sample I believe I can provide is the code that the service uses to launch the app:
void SpawnToastApp()
{
constexpr int nProcFlags = DETACHED_PROCESS | NORMAL_PRIORITY_CLASS;
constexpr wchar_t* wcsDesktop = L"WinSta0\\Default";
constexpr wchar_t* wcsToastApp = L"ToastApp\\Toast App (WIP).exe";
HANDLE hUser = NULL;
STARTUPINFOW si{ 0 };
wchar_t wcsCmdLine[MAX_PATH]{ 0 };
_snwprintf_s(wcsCmdLine, _TRUNCATE, L"\"%S\\%s\" %lu", _strInstallDir, wcsToastApp, GetCurrentProcessId());
_sessionCanToast = WTSQueryUserToken(_sessionId, &hUser);
if (_sessionCanToast)
{
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.lpDesktop = wcsDesktop;
_sessionCanToast = CreateProcessAsUserW(hUser, NULL, wcsCmdLine, NULL, NULL, FALSE,
nProcFlags, NULL, NULL,
&si, &_toastHandlerProcessInformation);
}
if(!_sessionCanToast)
{
/// Log it
}
if (hUser) { CloseHandle(hUser); };
}
I include the C++ code because I believe that I have narrowed the problem down to the launch method but am unsure the specific cause beyond that.
Additional Information:
These screenshots utilize Windows.UI.Notifications.ToastNotifications created from raw XML, but I have also tried using the Microsoft.Toolkit.Uwp.Notifications NuGet Package as recommended by Microsoft to the same effect.
I believe this project is a Windows Form App.
I am not using any sort of package--no APPX, MSIX, or sparse package. This is meant to be a lightweight app whose sole function is for toasts. While using a package isn't out of the question, suffice it to say that the number of hurdles and implementation issues make packaging this app undesirable. Indeed, the only reason I would want to package this app is for the icon in the upper left-hand corner of it, which it evidently does already, just not in the way I desire.
Similar to but NOT a duplicate of:
Change toast notification icon in wpf
I have already done this. My issue pertains to the icon's inconsistency rather than the lack of it entirely.
Why is app icon missing for toast notifications in action center on desktop?
I am using a Release build of my app
Cannot override notification app logo for Windows 10/11 VSTO app
Using AppOverrideLogo gets my icon to show under all circumstances, but it's more like a picture in the body of the toast rather than the small icon in the upper left-hand corner of the toast. Essentially, it's not the style I want.
EDIT 1:
I followed a sparse packaging guide found here to more or less the same result, the main difference being that now no icon not shows up at all anywhere. I used the asset generator in Visual Studio and then used the MSIX unpackaging tool to inspect the contents of the sparse package and confirmed it contained the generated assets. I had to comment out the reference to the splash screen because the app failed to register with that line included in the manifest.
EDIT 2:
I have decided to proceed with this app as if I am not having this issue, and so I used Visual Studio's Performance Profiler to analyze my app's resources. The Performance Profiler launched my toast app, and the toasts had the correct icon, so at this point I am 100% certain it has something to do with my service's launch method. Unfortunately, I am no closer to understanding why the icon does not show only when launched from my service.

How to create in xamarinforms/android what will remind you of a call in IOS?

I am creating an application in which I have calls, when a call occurs, my application shows a notification like in the picture below.
Pictures for example!!! I do for android
The problem is that the notification is hidden in the curtain on its own, although the call is still in progress, but I need it to be displayed all the time while the call is in progress. For example, like on the Iphone, something like this
Below is the standard code for building the appearance of a notification that I use.
I thought about how to extend the time when the notification is displayed on the screen, but have not found a solution yet ((
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();

How to resume where left off after sleep

I would like to make my Xamarin.Forms cross-platform app have the ability to resume after it sleeps.
I have been googling for a while but haven't found any solutions.
I tried reading this Xamarin tutorial but it doesn't seem to have anything for what I am trying to accomplish.
I tried looking for something like base.OnResume() but there is no base variable or any methods.
Xamarin Forms has an OnSleep and an OnResume method in the Application class
These can be used to save and restore variables when the app is backgrounded and resumed.
It is up to you to save the details you need such as page to load and state to restore. Look at the persist data section.
Here is a blog post about doing this.
You can find this methods in your app.xml file. Handle your code inside that file whatever you want to do onresume

How can I update my tiles in the background in Windows 8?

How can I update my tiles in the background while my app is not running in the foreground? I tried looking into push notifications, but I don't think that'll get me anywhere.
Where to begin? I know how to create tiles already.
An app cannot update it's tile without running at least once. Because apps do not run immediately on installation, it isn't possible for a tile to be "live" until it is launched. After app installation, the user needs to launch the app for it to set up and begin receiving updates in any form (whether they are push notifications, periodic updates, scheduled notifications, or local notifications raised with or without a background task). The default tile will be shown from the point the user installs the app until the time the app sets up tile updates.
MSDN has a good article on choosing the right notification delivery mechanism, which also links to related code samples:
http://msdn.microsoft.com/en-us/library/windows/apps/hh779721.aspx
As far as I know, you have to use a background task. If the user hasn't run the app once, you can't show any interactive tile data.
You have to use a TileNotification from the Windows.UI.Notifications namespace. The documentation for the namespace is here.
There's also an example Stocks app that uses notifications here (search for 'Tiles and notifications')
Further, there's an 'App tiles and badges' sample that shows both text and image updates to a live tile.
Adding Live tiles to the desktop using c# code:
List<Uri> StoryUrls = new List<Uri>();
StoryUrls .Add(new Uri("tiles.xml"));
TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
TileUpdateManager.CreateTileUpdaterForApplication().Clear();
TileUpdateManager.CreateTileUpdaterForApplication().StartPeriodicUpdateBatch(StoryUrls, PeriodicUpdateRecurrence.HalfHour);
The xml should be in the following format:
tiles.xml should follow microsoft format show in step 5 Here
Hope this helps

Properly dispose QuickTime control

I'm currently developing an application for Windows platform that will be able to play Quick Time videos.
The targeted OS versions are Windows XP, Windows Vista and Windows 7.
I successfully used the Apple ActiveX QuickTime Control 2.0 (in C#) and everything works well on Windows XP.
However, on Vista and Windows 7 I'm facing problems with properly disposing the control.
Here are the "steps to reproduce" :
- I create the Quick Time control dynamically at runtime and I place it on a panel;
I successfully play videos with it;
At some point I close the application main form; the application windows is destroyed and the application will continue to run in the background (doing operations like syncing videos); at this point the control is disposed using IDisposable pattern; I can't explicitly dispose the Movie object (from the ActiveX control) because I get an exception like: "COM object that has been separated from its underlying RCW cannot be used."; I just use (AxQTOControlLib.AxQTControl) player.Dispose(); On Windows XP this is fine but not on Vista and 7
I restore my application (from tool bar where it was running in background) and try to open the video again; At this point an AccessViolationException "Attempted to read/write protected memory" is thrown;
My questions are:
Why is this happening only on Vista and Windows 7 ?
On XP is a hidden leak ?
What is the recommended way to dispose the control (with its movie object) when it is created at runtime ?
I'm now using an explicit Movie.Disconnect() call (although I don't now what this disconnect means because I could not find proper documentation) fallowed by a QuickTimeTerminate() call before the form is closed.
While my method is working it is kind of design breaking so I would like to know a better way of doing this.
Thank you,
Mosu'
Update:
I just discovered that my method is not workink. I use to players: QuickTime control and Windows Media player control (both ActiveX) and when one fails to play a file the other one is used. I was seeing the output of WindowsMedia player and thinking the methos IS working.
So my fix is not working at all.
Lots of people seem to be having this error.
This page offers an interesting approach: http://www.theusenetarchive.com/usenet-message-how-to-properly-destroy-quicktime-activex-object-10384503.htm
The crash occurs because of file handles still being open. I was
'unloading' movies from the Quicktime control by wiring a null string
to the FileName or URL property. Quicktime doesn't handle this
correctly and leaves the original file open. So instead I wired
a path to an actual image file (Quicktime does images too) that was
all black. Eveything seems fine now.

Categories

Resources