I try to realize a shared project with Xamarin Forms for Android, ios and WinPhone. I want to use notifications and I followed this sample (https://github.com/edsnider/Xamarin.Plugins/tree/master/Notifier) and was able to create a local notification in Android. But i still do not understand how to work with that like Android application. Open a page or check if the page is already active or abort a broadcast for all platforms.
Any links that could help me? Am I correct that it only works with an Interface that i have to implement in each platform or does exist any other solution?
My basic problem is that I get messages from the xmpp-protocol and want to send message notifications to the user or update the ui if open...
Thanks for your help...
You do not have to add any interfaces to your platform projects, Just intall nuget package to both projects Android/IOS.
on IOS you should add:
// Ask the user for permission to get notifications on iOS 8.0+
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
var settings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet ());
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
}
to your method:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
in AppDelegate:
Then in your shared project you call:
using Plugin.LocalNotifications.Abstractions;
CrossLocalNotifications.Current.Show("You've got mail", "You have 793 unread messages!");
As of replacing your current page, you do not have to do anything, Xamarin Forms will handle that for you.
Also on android, you can controll, you application relaunch behavior by setting LaunchMode of your MainActivity:
Example:
LaunchMode = Android.Content.PM.LaunchMode.SingleInstance, AlwaysRetainTaskState = true
To read more about LauchModes on Android, please read:
https://developer.android.com/guide/components/tasks-and-back-stack.html
And check additionl activity flags:
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
Currently this plugin does not allow to catch any notification inside app.
But you can do this, by using platform specific functionality
IOS:
Please refer to
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
MessagingCenter.Send<MainPage> (this, "IOS notification received");
}
Android:
After reading through source of this plugin, i've discovered, that youcan check Bundle in your on create method for key ScheduledAlarmHandler.LocalNotificationKey
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if(bundle.ContainsKey(ScheduledAlarmHandler.LocalNotificationKey)){
//App launched form notification
MessagingCenter.Send<MainPage> (this, "Android notification received");
}
}
Also you can try to use BroadcastReceiver class;
https://developer.xamarin.com/api/type/Android.Content.BroadcastReceiver/