Launch apps from the band - microsoft-band

Theory: Use Cortana on the band to launch your application on the phone, and that application will communicate with the band, allowing your band custom tile/buttons to work.
I have it working when you use Cortana on the phone, BUT when using Cortana on the band it does try to launch the app, but it hangs on Starting (app name) with your app icon (basically Cortana trying to start the app). I don't even think it makes it to the app.xaml..
This is where I need help...
Use this page to get Cortana to launch your simple application...
https://msdn.microsoft.com/en-us/library/windows/apps/mt185609.aspx?f=255&MSPPError=-2147217396
You have to put code in your app.xaml.cs as well to navigate to the main page when coming in via voice commands.
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.VoiceCommand)
{
Frame rootFrame = Window.Current.Content as Frame;
// if (rootFrame == null)
//{
// Repeat the same basic initialization as OnLaunched() above, taking into account whether
// or not the app is already active.
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();
rootFrame.NavigationFailed += OnNavigationFailed;
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// 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
rootFrame.Navigate(typeof(MainPage));
}
Window.Current.Activate();
}
// }
}

Related

Xamarin: iOS remote notification has no sound

I've been banging my head for a few days now trying to figure this out.
I followed this article to setup local notifications for both iOS and Android
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/local-notifications
I also added push notification service for both platforms, on Android everything works fine (both local and remote push notifications).
For iOS, the article doesn't mention that you have to add a line of code to make the notification alert with sound, so I modified the SendNotification method to have the content object like this
var content = new UNMutableNotificationContent()
{
Title = title,
Subtitle = "",
Body = message,
Badge = 1,
Sound = UNNotificationSound.Default
};
Now the notifications have sound on iOS for both local and remote notifications, but when the app is closed (in background) the notification still shows without sound.
What am I missing?
You could add the following code in the AppDelegate.cs file :
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
...
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
// you could register different settings
var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
);
app.RegisterUserNotificationSettings(notificationSettings);
}
...
}
The code above ask for the permisson to display notifications and we could register different settings as we like. So the following code in Intialize method in Xamarin LocalNotifications sample code is not needed any more as well as the bool hasNotificationsPermission .
public void Initialize()
{
// the following code could be deleted
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
hasNotificationsPermission = approved;
});
}
Hope it works for you.

Xamarin.forms NotificationTapped event not fired when tap on notification

I am using Plugin.LocalNotification to create notification after file is downloaded. But my issue is I am not able to open that file by tapping notification. Instead it navigates to MainPage(Login Page in my case)
public App()
{
InitializeComponent();
NotificationCenter.Current.NotificationTapped += Current_NotificationTapped;
}
private void Current_NotificationTapped(NotificationTappedEventArgs e)
{
// This event doesn't get fire.
}
I show Notification using following code after creating pdf file.
var notification = new NotificationRequest
{
Title = "Invoice",
Description = message,
ReturningData = filepath, // Returning data when tapped on notification.
// NotifyTime = DateTime.Now.AddSeconds(2) // Used for Scheduling local notification, if not specified notification will show immediately.111
};
NotificationCenter.Current.Show(notification);
NotificationCenter.Current.NotificationTapped += Current_NotificationTapped; // tried calling tapped event here as well. but no success.
private void Current_NotificationTapped(NotificationTappedEventArgs e)
{
}
Someone has also reported bug regarding this issue on github.
Thanks

Xamarin Forms: How to launch a UWP app from another UWP app?

I am trying to launch my UWP app from another UWP app. This feature is working fine for android and ios.
I am using this blog for the UWP section.
UWP Render Code
public class OpenAppImplementation : IAppHandler
{
public async Task<bool> LaunchApp(string stringUri)
{
Uri uri = new Uri(stringUri);
return await Windows.System.Launcher.LaunchUriAsync(uri);
}
}
In the Main project
var appname = "UWP package name://";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
if (!result)
{
Device.OpenUri(new Uri(windows store link));
}
I am passing the package name of the second app as appname here. If the app is installed in the device, I need to open that; else open the windows store app page for downloading the app.
When I execute this I am getting a screen like below:
Update
I have added the protocol declaration on the second app as below:
Then on the first app call like below:
var appname = "alsdk:";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
if (!result)
{
Device.OpenUri(new Uri("windows store link"));
}
The second app is opening and the splash screen is showing after the above changes. But not going to the home page, the app is staying in the splash screen. Also, I need to show the windows store app page if the app is not installed on the device. What I am missing here?
The problem is you have pass the wrong uri scheme or you have not registered protocol for the launched app. For more please refer this document.
Steps:
Specify the extension point in the package manifest like the following.
<Applications>
<Application Id= ... >
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="alsdk">
<uap:Logo>images\icon.png</uap:Logo>
<uap:DisplayName>SDK Sample URI Scheme</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
...
</Application>
And you could also use visual studio manifest GUI to add the protocol.
If you have not specific the parameter you could remove // , just keep uri scheme like alsdk:.
Update
The second app is opening and the splash screen is showing after the above changes. But not going to the home page, the app is staying in the splash screen. Also, I need to show the windows store app page if the app is not installed on the device. What I am missing here?
In target app we need process OnActivated method and invoke Window.Current.Activate() finally.
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
// Navigate to a view
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
// assuming you wanna go to MainPage when activated via protocol
rootFrame.Navigate(typeof(MainPage), eventArgs);
}
Window.Current.Activate();
}
Update
if you launch xamarin uwp app, you need initialize Xamarin Form component in the OnActivated method.
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
// Navigate to a view
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Xamarin.Forms.Forms.Init(args);
Window.Current.Content = rootFrame;
}
// assuming you wanna go to MainPage when activated via protocol
rootFrame.Navigate(typeof(MainPage), eventArgs);
}
Window.Current.Activate();
}

Xamarin Forms iOS Location Permission(Always Allow)

I am using (Always Allow) location permission in my iOS Xamarin Forms application. The problem I face today is that I'm unable to fully leverage ALWAYS permission in order to bring back my app (which was already killed) in the background whenever a location event occurs. Any Solution for that ?
My Code :
lm = new CLLocationManager
{
PausesLocationUpdatesAutomatically = false,
DesiredAccuracy=CLLocation.AccuracyBest,
DistanceFilter=0,
AllowsBackgroundLocationUpdates = true
};
lm.LocationsUpdated +=
(object sender, CLLocationsUpdatedEventArgs e) => {
var locations = e.Locations;
LocationEventArgs args = new LocationEventArgs();
args.lat = locations[locations.Length - 1].Coordinate.Latitude;
args.lng = locations[locations.Length - 1].Coordinate.Longitude;
locationObtained(this, args); //Log
};
lm.AuthorizationChanged += (object sender,
CLAuthorizationChangedEventArgs e) => {
if (e.Status == CLAuthorizationStatus.Authorized)
{
//lm.StartMonitoringSignificantLocationChanges();
lm.StartUpdatingLocation();
}
};
lm.RequestAlwaysAuthorization();
Thank you
The problem I face today is that I'm unable to fully leverage ALWAYS permission in order to bring back my app (which was already killed) in the background whenever a location event occurs.
If APP terminated , AllowsBackgroundLocationUpdates will not work. You should ensure app is not killed first.
allowsBackgroundLocationUpdates : A Boolean value indicating whether the app should receive location updates when suspended.(Not killed status).

Appcenter Push: Unable to display notification

I am trying to implement a event handler to see if a custom data attribute is present and if so remove some app data. The issue is now that no due to the event delegate i cannot show the notifications that do not match the custom data value.
I need to display the push notification if it doesn't have the custom data key. How can i do this in Xamarin forms?
private void SetupPushNotificationHandle()
{
// This should come before AppCenter.Start() is called
// Avoid duplicate event registration:
if (!AppCenter.Configured)
{
Push.PushNotificationReceived += (sender, e) =>
{
OceanBusiness business = CoreDependencyService.GetBusinessLayer<OceanBusiness>();
// If there is custom data associated with the notification,
// print the entries
if (e.CustomData != null)
{
foreach (var key in e.CustomData.Keys)
{
switch (key)
{
case CoreSettings.ClearData:
{
if(key.ToLower() == "true")
business.RemoveData();
break;
}
}
}
}
};
}
I have seen how to do this in Xamarin.Android but not in Xamarin.iOS. As such answers that do not show how to do both will not address this question.

Resources