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/
Related
How can I implement SfMaps in my project which is based on MVVM structure? 1st I have to get my current location and show it on the map then I have to drag that marker point anywhere and set it as the current location
Add SfMaps to your project from NuGet. In android no need for native configurations, but to launch the SfMaps in iOS, call the SfMapsRenderer.Init() in the FinishedLaunching overridden method of the AppDelegate class
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
…
global::Xamarin.Forms.Forms.Init ();
Syncfusion.SfMaps.XForms.iOS.SfMapsRenderer.Init();
LoadApplication (new App ());
…
}
Add the following namespace in your view file.
xmlns:maps="clr-namespace:Syncfusion.SfMaps.XForms;assembly=Syncfusion.SfMaps.XForms"
And add the below code to initializing map in view
<maps:SfMaps >
</maps:SfMaps>
this is a very basic way to add SFMap in Xamarin. forms for more details and help
please visit this link: https://help.syncfusion.com/xamarin/maps/getting-started?cs-save-lang=1&cs-lang=xaml
I am using App Center for Push Notification in xamarin forms.
I have followed all the steps required for App Center as well as Firebase.
But still i am getting null value for install id
var guid = AppCenter.GetInstallIdAsync().Result
Need your help to debug this.
InstallId on Xamarin Android is null if called before the start method of the SDK.
Dont forget to update Appcenter Nuget packages, there was a specific old version with a bug on the GetInstallIdAsync method
Another thing: dont use .Result. Await the method in your Onstart method like this:
protected override async void OnStart()
{
AppCenter.Start("ios=-----;android=----", typeof(Push));
var guid = await AppCenter.GetInstallIdAsync();
}
Since OnStart is just an event, and nothing is waiting for it’s return, using async void is acceptable here.
I'm doing this in my apps and it works
I'm actually working on a Xamarin Forms application that need push notifications. I use the Plugin.PushNotification plugin.
When the app is running in the foreground or is sleeping (OnSleep), I have no problem to open a specific page when I click on a notification that I receive. But I was wondering how can I do that when the app is closed. Thanks!
I finally found the answer by myself and I want to share it in case someone needs it.
Nota bene: according to the official documentation of the plugin, it's Xam.Plugin.PushNotification that is deprecated. I use the new version of this plugin, Plugin.PushNotification which uses FCM for Android and APS for iOS.
There is no significant differences to open a notif when the app is running, is sleeping or is closed. Just add the next callback method in the OnCreate method (MyProject.Droid > MainApplication > OnCreate) and FinishedLaunching method (MyProject.iOS > AppDelegate > FinishedLaunching):
CrossPushNotification.Current.OnNotificationOpened += (s, p) =>
{
// manage your notification here with p.Data
App.NotifManager.ManageNotif(p.Data);
};
Common part
App.xaml.cs
// Static fields
// *************************************
public static NotifManager NotifManager;
// Constructor
// *************************************
public App()
{
...
NotifManager = new NotifManager();
...
}
NotifManager.cs
public class NotifManager
{
// Methods
// *************************************
public void ManageNotif(IDictionary<string, object> data)
{
// 1) switch between the different data[key] you have in your project and parse the data you need
// 2) pass data to the view with a MessagingCenter or an event
}
}
Unfortunately there is no succinct answer for either platform. Generally speaking, you need to tell the OS what to do when it starts the app as a result of the push notification. On both platforms, you should also consider what API level you are targeting, otherwise it won't work or even crash the app.
On iOS, you will need to implement this method in AppDelegate appropriately: FinishedLaunching(UIApplication application, NSDictionary launchOptions). The launchOptions will have the payload from the push notification for you to determine what to do with it (e.g. what page to open). For more information on iOS, Xamarin's documentation is a good place to start.
Android has a more complicated topology in terms of more drastic differences between API levels, whether you are using GCM/FCM, as well as requiring more code components. However, to answer the question directly, you will need to handle this in OnCreate(Bundle savedInstanceState) of your main Activity. If you are using Firebase, the push notification payload is available in Intent.Extras. Again, Xamarin's documentation has a good walkthrough.
Finally, note that the Plugin.PushNotification library you are using has been deprecated. I suggest you either change your library and/or your implementation soon. Part of the reason that library has been deprecated is because Google has deprecated the underlying Google Cloud Messaging (GCM) service, which will be decommissioned on April 11, 2019.
I create some project that consists from videoplayer. For Xamarin android I implemented through messaging center.
On android:
Page(xamarin forms) -> (click button and sending message to MainActivity) -> calling MyVideoActivity via intent
With android all works perfectly.
But I am newbie with ios and xamarin ios also.
And I don't know is this real to implement the same in xamarin ios through messaging center ? Also I would like use .xib file(with ui, taken from Xcode). I am not sure how to implement it. I would like to do it through messaging center -> calling custom ViewController with my .xib file. Is it real?
p.s. I've found some example with PageRenderer, but don't investigate it. I am not sure that it will be suitable for me.
Call the following methods in your MessagingCenter.Subscribe lambda:
XIB-based ViewController:
var vc = new XIBBasedViewController();
// Set any public properties / call methods on your `XIBBasedViewController`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Storyboard-based ViewController:
var storyboard = UIStoryboard.FromName("StoryBoardFileNameMinusExtension", null);
var vc = storyboard.InstantiateViewController("AStoryBoardIDAssignedToYourViewController") as AViewControllerSubClass;
// Set any public properties / call methods on your `AViewControllerSubClass`
InvokeOnMainThread(() => UIApplication.SharedApplication.Windows[0].RootViewController.PresentViewController(vc, true, null));
Note: A good place to setup your messaging center subscribes is in the FinishedLaunching of the AppDelegate class.
I am working on facebook authentication in a Xamarin forms app and want to use "native" login on each device.
The Facebook SDK (by Xamarin) is not designed for forms since it requires passing in an Android activity that implements some specific interfaces.
However; I was able to get the UI to display using a custom page renderer and then in that renderer calling StartActitivty with an activity that uses the exact implementation described in the Facebook SDK getting started. https://components.xamarin.com/view/facebookandroid
So far everything works perfect. The android app starts, xamarin forms kicks in, the custom page renderer is loaded the login android activity starts and the user logs in with facebook and we get to the console.writeline below.
So, how do I dismiss this intent or otherwise get back to xamarin forms?
Do I:
Dismiss this intent? - if so then what?
Inject something to "reset" the main page?
Other?
public void OnCompleted (Xamarin.Facebook.Model.IGraphUser user, Response response)
{
//TODO: show user details with welcome and button that takes them to main app.
//TODO: switch back to xam forms from here on out.
//TODO: figure out how to change the `main` activity to xam forms.
// 'Me' request callback
if (user != null) {
//How do I get back to Xamarin forms from here?
Console.WriteLine ("GOT USER: " + user.Name);
} else {
Console.WriteLine ("Failed to get 'me'!");
}
}
You should call Finish();
Anyway, if you want to see exactly how I did it, you can check it here:
https://github.com/IdoTene/XamarinFormsNativeFacebook