UWP C# - Re-launch app from notification click - c#

I'm writing a UWP app, and I have a ScheduledToastNotification that is added to the schedule when the app is suspended (e.g. like a reminder). However, if I close the app, the notification appears on time, but when I click on the notification (no buttons, just on the notification in general), the app doesn't launch correctly, stopping at the splash screen.
How do I get the app the re-launch correctly?
Thanks.

You should override OnActivated in App.Xaml.cs and handle this like
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.ToastNotification)
{
var toastArgs = args as ToastNotificationActivatedEventArgs;
var arguments = toastArgs.Argument;
if (arguments == "ARG")
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
rootFrame.Navigate(typeof(YOURPAGE));
Window.Current.Activate();
}
}
}

Related

Error during UWP app launch in Windows 10 mobile

I have an UWP app with a lot of errors in dev center console during launch such as this:
em_watchdog_timeout_deada444_514cabuxamapache.391043fc20bb3_fa4730peekfge!lockscreenimages.exe_timeout_expired:_event_type_=_targetstatechanged,_timeout_modifier_type_=_none,_server_task_currentstate_=_navigatingto,targetstate=_active.
I suspect it's due to Cortana or Analitycs activation in "App.cs":
private async Task SetupVoiceCommands()
{
try
{
StorageFile vcdStorageFile = await Package.Current.InstalledLocation.GetFileAsync(#"Commands.xml");
await VoiceCommandDefinitionManager.InstallCommandDefinitionsFromStorageFileAsync(vcdStorageFile);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Installing Voice Commands Failed: " + ex.ToString());
}
}
private void InitAnalyticsTracker()
{
GoogleAnalyticsTracker = AnalyticsManager.Current.CreateTracker("UA-XXXXXXXX");
AnalyticsManager.Current.ReportUncaughtExceptions = true;
AnalyticsManager.Current.AutoAppLifetimeMonitoring = true;
AnalyticsManager.Current.IsDebug = false;
}
This code is executed in:
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
Window.Current.Content = rootFrame;
}
if (e.PrelaunchActivated == false)
{
await SetupVoiceCommands();
if (rootFrame.Content == null)
{
InitAnalyticsTracker();
rootFrame.Navigate(typeof(Shell), e.Arguments);
}
else
{
var page = rootFrame.Content as Shell;
page?.OnLaunchedEvent(e.Arguments);
}
Window.Current.Activate();
CustomizeStatusBar();
}
}
A lot of users say the app does not even start...
Any ideas please?
The call await SetupVoiceCommands(); blocks the rest of the code in the OnLaunched method: until the execution of SetupVoiceCommands() is finished the main page of the app won't be displayed (which is supposed to happen within a short period of time after the app launch, otherwise the system will shut down your app as not responding).
Consider moving the await SetupVoiceCommands(); closer to the end of the OnLaunched method, e.g. after CustomizeStatusBar();.
To get a better understanding of how it affects the flow of execution and the launch time of the app, you could replace the call await SetupVoiceCommands(); with await Task.Delay(5000); to imitate the delay and then try moving it around the OnLaunched method as suggested.

Cordova windows : identify the launched URI

I have 2 windows apps in which one is native and one is cordova windows app.
user will be having some set of navigations from windows native app which would invoke the respective page in cordova app depending up on the URI invoked from native app.
i am actually new to cordova. The similar thing in native i should be able to handle using below code
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
var _args = args as ProtocolActivatedEventArgs;
argValues.Add(_args.Uri.ToString());
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), argValues);
}
Window.Current.Activate();
// TODO: Handle URI activation
// The received URI is eventArgs.Uri.AbsoluteUri
}
}
The above app lifecycle event is been added to Application class(app.xaml). But didn't saw similar application class in cordova window app. If application class can be added to Cordova I would like to develop a similar plugin like cordova-webintent https://github.com/Initsogar/cordova-webintent
I saw one corodva windows plugin but it supports only for win8 Phone
cordova-urlhandler-plugin

Cortana integration faliure

I am making a Windows 8.1 speech based application. My problem is that when I give Cortana the input it launches my app and the app closes at the splashscreen, but when I run my app in background (minimize the app) or when the app is running, the Cortana input works perfect.
Where am I going wrong? Here is my app.xaml.cs code, in the OnActivatedMethod:
if (args.Kind == ActivationKind.VoiceCommand)
{
VoiceCommandActivatedEventArgs vcArgs = (VoiceCommandActivatedEventArgs)args;
string voiceCommandName = vcArgs.Result.RulePath.First(); // What command launched the app?
switch (voiceCommandName) // Run the action specific to the command
{
case "comand1": // User said comand1
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand2": // User said comand2
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand3": // User said comand3
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
case "comand4":
rootFrame.Navigate(typeof(SpeechHandlingPage), vcArgs.Result);
break;
}
and in the OnNavigated method of the speech handling page:
SpeechRecognitionResult vcArgs = e.Parameter as SpeechRecognitionResult;
RcvdCommand = vcArgs.Text.ToUpper();
// Check for null!
string commandMode = vcArgs.SemanticInterpretation.Properties["commandMode"][0];
if (commandMode == "voice") // Did the user speak or type the command?
{
RequestHanding();
MyTextblock.Text = vcArgs.Text;
// SpeakText(audioPlayer, String.Format(" app heard you say {0}", RcvdCommand ));
// HandleNlpCommand(vcArgs);
}
else if (commandMode == "text")
{
// messageTextBox.Text = string.Format("Working on your request \"{0}\"", RcvdCommand);
// HandleNlpCommand(vcArgs);
}
When the application is not running, and is activated by voice command, the OnLaunched() method is not called. So you need to call the code that ensures the root frame is created in the OnActivated() method as well:
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
// ... restore app state, etc.
Window.Current.Content = rootFrame;
}

How to navigate to a page after the splash screen loads and before the grid template app loads C# XMAL Windows 8 store app?

this is my first time to use stackoverflow :) ,
I was working with grid template app in windows 8 store app and i wanted to add welcome page after the splash screen loaded , to do that i tried to add this statement
this.Frame.Navigate(typeof(WelcomePage));
to the OnLaunched method that was created by default with the grid template in the App.xmal.cs. after i created the Welcomepage it self and i created a button in the welcome page to navigate to the GroupedItemsPage.
My logic isn't working.
this is the default method code in the App.xmal.cs if that will help
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
//Associate the frame with a SuspensionManager key
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Restore the saved session state only when appropriate
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (!rootFrame.Navigate(typeof(GroupedItemsPage), "AllGroups"))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}

Set startup form for Store App

I made a C# store app in Visual studio 2013. All of the sudden the startup form is changed, so its no longer the Mainpage. I cannot find how to resolve this problem.
I found a link where I could do something in the properties of my project, but I was unable to resolve the problem.
Does anyone know how to change the startup form for a C# store app in Visual Studio 2013?
edit: here is the OnLaunched method as told in comments.
protected override async void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
Frame rootFrame = Window.Current.Content as Frame;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
await CatalogApp.Common.SuspensionManager.RestoreAsync();
}
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
CatalogApp.Common.SuspensionManager.RegisterFrame(rootFrame, "appFrame");
// Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
}
// Place the frame in the current Window+
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
There is probably is a navigation statement like: rootFrame.Navigate(typeof(ItemsPage), e.Arguments); in the
protected override async void OnLaunched(LaunchActivatedEventArgs e)
override in App.xaml.cs
If you are using this default, check typeof(ItemsPage) and the e.Argument statements.
Keep in mind, by default at activation,
the application redirects to the last opened window.
UPDATE
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
rootFrame.Navigate(typeof(MainPage), e.Arguments);
is a double navigation: first navigate to ShowItems, if succeeded navigate to MainPage. Why is that? I think this will lead to unexpected results.
Please try to remove
if (!rootFrame.Navigate(typeof(ShowItems), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
and check the results.
By the way: Which one is the page you want to start with?

Categories