Xamarin iOS handleAction never call - xamarin

I have an Xamarin.Forms app where I want to use push notifications. I used azure push notification hub from mobile service to push the message. I want to create an interactive banner with some action "Action1" and "Action2" on iOS. I am able to receive the push message with "Action1" and "Action2" button. But tapping on that button does not do any thing. following is my code:
private static void RegisterPushAction()
{
UIMutableUserNotificationAction acceptAction = new UIMutableUserNotificationAction ();
acceptAction.Title = "Action1";
acceptAction.Identifier = "ACCEPT_IDENTIFIER";
acceptAction.ActivationMode = UIUserNotificationActivationMode.Background;
acceptAction.Destructive = false;
acceptAction.AuthenticationRequired = false;
UIMutableUserNotificationAction denyAction = new UIMutableUserNotificationAction ();
denyAction.Title = "Action2;
denyAction.Identifier = "DENY_IDENTIFIER";
denyAction.ActivationMode = UIUserNotificationActivationMode.Background;
denyAction.Destructive = false;
denyAction.AuthenticationRequired = false;
UIMutableUserNotificationCategory acceptCategory = new UIMutableUserNotificationCategory ();
acceptCategory.Identifier = "JOIN_CATEGORY";
acceptCategory.SetActions (new UIUserNotificationAction[]{acceptAction,denyAction}, UIUserNotificationActionContext.Default);
NSSet categories = new NSSet (acceptCategory);
//iOS 7
if (Convert.ToInt16 (UIDevice.CurrentDevice.SystemVersion.Split ('.') [0].ToString ()) < 8) {
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
} else {
UIUserNotificationType types = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes (types, categories);
UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
}
}
To handle this I have following method:
public override void HandleAction (UIApplication application, string actionIdentifier, NSDictionary remoteNotificationInfo, Action completionHandler)
{
if (actionIdentifier.Equals ("ACCEPT_IDENTIFIER")) {
//var alert = notification.AlertBody;
//new UIAlertView ("Msg", alert, null, "OK", null).Show ();
//NotificationHelper.ReceivePushMessage (alert);
ProcessNotification (remoteNotificationInfo, false);
}
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
completionHandler ();
}
The above HandleAction method gets never called and it always calls ReceivedRemoteNotification
public override async void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
ProcessNotification (userInfo, false);
}
I have different types of notificatiosn (e.g. a simple banner notification and a banner with Action)
Any thoughts on this?

I do not know if you still have the problem but you should try to write the function HandleAction like this:
public override void HandleAction (UIApplication application, String actionIdentifier, NSDictionary remoteNotificationInfo, NSDictionary responseInfo, Action completionHandler)
Let me know if it worked!

Related

How to do a Close Confirmation with a Xamarin Forms mac App?

I have a Xamarin.Forms application for iOS, Android, and now hopefully Mac. I made all the adjustments for the UI to look great on Mac. Submitted it for approval where it was rejected because the user can close the window while the app and menu bar is still running. So I figure I would just add a confirmation pop-up asking if they want to exit the app when they try to close the window.
OK = Terminate the App.
Cancel = Keep the window open.
I find lots of articles on how to handle this with a Xamarin.Mac app, but nothing on how to handle Xamarin.Forms on Mac. The FormsApplicationDelegate does not give access to the View Controller or the Window Delegate in order to override the WindowShouldClose method. I found that I can use NSAlert to do the pop-up which works great. Now I cannot find anything on what to do when the user responds. Open to suggestions.
private void Window_WillClose(object sender, System.EventArgs e)
{
NSNotification senderNotification = ((NSNotification)sender);
NSWindow closingWindow = (NSWindow)senderNotification.Object;
var confirmation = new NSAlert()
{
AlertStyle = NSAlertStyle.Warning,
InformativeText = "Are you sure you want to exit the App?",
MessageText = "Exit?"
};
confirmation.AddButton("OK");
confirmation.AddButton("Cancel");
var result = confirmation.RunModal();
if (result == 1001)
{
//Cancel closing the window
}
else
{
//terminate the app
}
}
After a lot of experimenting, I did find a solution. Here is what officially passed Apple's review. It requires that n menu action is linked as "New Window". It keeps tracks of the open windows and when there is only one left, it prompts to close the app. If the user closes all the windows and keeps the app running, they have the option to open a new window in the menu.
[Register("AppDelegate")]
public class AppDelegate : FormsApplicationDelegate
{
public NSWindow window;
private bool closeApp;
private List<NSWindow> openWindows;
public override NSWindow MainWindow
{
get { return window; }
}
public AppDelegate()
{
this.closeApp = false;
this.openWindows = new List<NSWindow>();
createNewWindow();
}
[Action("newWindow:")]
public void newWindow(NSObject sender)
{
createNewWindow();
this.window.MakeKeyAndOrderFront(sender);
LoadApplication(new App());
base.DidFinishLaunching(null);
}
private void createNewWindow()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new MainWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "MyApp"; // choose your own Title here
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
window.WillClose += Window_WillClose;
openWindows.Add(window);
}
private void Window_WillClose(object sender, System.EventArgs e)
{
openWindows.Remove((NSWindow)((NSNotification)sender).Object);
if (openWindows.Count == 0)
{
var confirmation = new NSAlert()
{
AlertStyle = NSAlertStyle.Warning,
InformativeText = "Do you want to exit the app?",
MessageText = "Exit?"
};
confirmation.AddButton("Yes");
confirmation.AddButton("No");
var result = confirmation.RunModal();
if (result == 1001)
{
this.closeApp = false;
}
else
{
//terminate the app
this.closeApp = true;
}
}
}
public override bool ApplicationShouldTerminateAfterLastWindowClosed(NSApplication sender)
{
return closeApp;
}
public override void DidFinishLaunching(NSNotification notification)
{
Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}
}

Pop the UIViewController in xamarin ios

I want to open a UIViewController from a content page and this code helps me in doing this
UIWindow Window = new UIWindow(UIScreen.MainScreen.Bounds);
var cvc = new ScanningPage();
var navController = new UINavigationController(cvc);
Window.RootViewController = navController;
Window.MakeKeyAndVisible();
but I also want a Back button on that Controller Page which navigate me back to that content Page.
this.NavigationController.PopViewController(true);
or
this.DismissViewController(true,null);
not working in this case.
I would recommend not creating a new Window and a UINavigationController...
Xamarin.Forms is contained in a single VC in the first Window, so you can obtain that view controller via:
UIApplication.SharedApplication.Windows[0].RootViewController;
So as an example Dependency service that presents and dismisses a VC (either from Forms or the view controller), you can do something like this.
Dependency interface:
public interface IDynamicVC
{
void Show();
void Dismiss();
}
iOS Dependency implementation
public class DynamicVC : IDynamicVC
{
UIViewController vc;
public void Show()
{
if (vc != null) throw new Exception("DynamicVC already showing");
vc = new UIViewController();
var button = new UIButton(new CGRect(100, 100, 200, 200));
button.SetTitle("Back", UIControlState.Normal);
button.BackgroundColor = UIColor.Red;
button.TouchUpInside += (object sender, EventArgs e) =>
{
Dismiss();
};
vc.Add(button);
var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
rootVC.PresentViewController(vc, true, () => { });
}
public void Dismiss()
{
vc?.DismissViewController(true, () =>
{
vc.Dispose();
vc = null;
});
}
}

Xamarin.Forms - Toasts(Banners) - Alerts - in iOS and Android in Background

I try to show toasts(banners) notification - while application in the background.
I checked background fetch and push notification. I also received push notification when app in foreground.
But when my app in background - I only see how change my badges count - but i don't see notification
This is my code:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init ();
LoadApplication (new App ());
if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet ());
UIApplication.SharedApplication.RegisterUserNotificationSettings (pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
} else {
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (notificationTypes);
}
return base.FinishedLaunching (app, options);
}
and
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
NSDictionary dic = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
var test = (dic[new NSString("test")] as NSString).ToString();
var alert = new UIAlertView ("Test title", "This is message", null, "Ok", "Button");
alert.Clicked += (object sender, UIButtonEventArgs e) => {Console.WriteLine(e.ButtonIndex);};
alert.Show ();
}
So how i say - in foreground all works great but in background I only see the new count of badges.
I also find this plugin:
https://components.xamarin.com/view/alert-center
But I want to understand how to write it without plugin.
Thank you.
Ok - I solved it !
For developers who will take same problem:
I just don't send ALERT object in aps json to ios.

Set ItemSize of UICollectionViewFlowLayout to width of device

Using Xamarin, I am trying to create a UICollectionViewFlowLayout where my cells will be the width of the device and flow down vertically. Something like the Facebook news tab iOS app.
Currently to get the Cells in the layout, here is by code:
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
var layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
Logger.Log(""+CollectionView.Bounds.Width);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
I want to use this to set the size of the item:
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
But CollectionView does not exist yet when i am creating the UICollectionViewFlowLayout,
Is there any other way i can set the size of UICollectionViewFlowLayout to the width of the device?
I found a solution to my problem. I created the layout as a global variable and then added it to the view in ViewDidLoad (), then in ViewWillAppear(..) i can set layout.ItemSize
So my code will as follows:
UICollectionViewFlowLayout layout;
public override async void ViewDidLoad ()
{
try{
base.ViewDidLoad ();
Title = "News";
layout = new UICollectionViewFlowLayout ();
layout.MinimumInteritemSpacing = 30;
var collectionView = new NewsListView (UIScreen.MainScreen.ApplicationFrame, layout);
collectionView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
collectionView.BackgroundColor = UIColor.LightGray;
collectionView.RegisterClassForCell (typeof(NewsListViewCell), cellId);
collectionView.ReloadData ();
CollectionView = collectionView;
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
public override async void ViewWillAppear (bool animated)
{
try{
base.ViewWillAppear (animated);
Logger.Log(""+CollectionView.Bounds.Width);
layout.ItemSize = new CGSize (CollectionView.Bounds.Width-20, 200.0f);
}catch(Exception ex){
Logger.Log (ex);
ShowUserMessage ("Error",ex.Message);
}
}
This way the item width of the layout is always based on device width and not hardcoded

Monotouch UITableviewCells never destroyed

I have a controller which has a UITableView named WaitTableView. It has only one cell, here is the code of the UITableViewCell class :
public class TableViewWaitCell : UITableViewCell
{
public UIActivityIndicatorView activityIndicator = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.Gray);
public UILabel lblLoading = new UILabel();
public TableViewWaitCell(UITableViewCellStyle style, string reuseIdentifier) : base (style, reuseIdentifier)
{
this.SelectionStyle = UITableViewCellSelectionStyle.None;
}
~TableViewWaitCell(){
System.Console.WriteLine("TableViewWaitCell.~TableViewWaitCell");
lblLoading = null;
activityIndicator = null;
System.GC.Collect();
}
protected override void Dispose (bool disposing){
System.Console.WriteLine("TableViewWaitCell.Dispose");
lblLoading = null;
activityIndicator = null;
base.Dispose (disposing);
GC.Collect();
}
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
var context = UIGraphics.GetCurrentContext();
var gradient = new CGGradient(
CGColorSpace.CreateDeviceRGB(),
new float[] { 1f, 1f, 1f, 1f,
0.68f, 0.68f, 0.72f, 1f },
new float[] { 0f, 1f } );
context.DrawLinearGradient(gradient,
new PointF(rect.X+rect.Width/2, rect.Y),
new PointF(rect.X+rect.Width/2, rect.Y+rect.Height),
CGGradientDrawingOptions.DrawsAfterEndLocation);
var activityIndicatorViewFrame = new RectangleF(rect.X + rect.Width/2-10, rect.Y+10, 20, 20);
this.activityIndicator .Frame = activityIndicatorViewFrame;
this.activityIndicator.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
this.activityIndicator.StartAnimating();
this.AddSubview(this.activityIndicator);
var labelFrame = new RectangleF(rect.X, rect.Y+10+activityIndicatorViewFrame.Height, rect.Width, 35);
this.lblLoading.Frame = labelFrame;
this.lblLoading.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
this.lblLoading.TextColor = UIColor.Black;
this.lblLoading.BackgroundColor = UIColor.Clear;
this.lblLoading.TextAlignment = UITextAlignment.Center;
this.lblLoading.Text = Dictionary.GetValue("Loading");
this.AddSubview(this.lblLoading);
}
}
here is the ViewWillDisappear method of the main UIViewController :
public override void ViewWillDisappear (bool animated)
{
Console.WriteLine("SpotlightView.ViewWillDisappear");
if(this.PopoverController!=null)
this.PopoverController.Dismiss(true);
this.PopoverController = null;
this.tableView.RemoveFromSuperview();
this.WaitTableView.RemoveFromSuperview();
this.searchBar.RemoveFromSuperview();
this.tableView.Source = null;
this.tableView.Dispose();
this.tableView = null;
this.WaitTableView.Source = null;
this.WaitTableView.Dispose();
this.WaitTableView = null;
this.searchBar.Delegate = null;
this.searchBar.Dispose();
this.searchBar = null;
base.ViewWillDisappear (animated);
}
My problem is that neither the destructor nor the Dispose of my cells got called. When I run heapshot the number of instances of the TableViewWaitCell class grow up has I navigate through my app. I don't understand how the cells life-cycle is managed in Monotouch, what could have I done wrong ?
I do not see anything that would cause this problem in the code you shared. However you do not show how the cell is constructed and stored. Your objects may be kept alive by a root you are not showing in your sample code. I have created a sample below that shows Dispose and the finalizer being called.
Using the simulator the table view and the cell are collected quickly. Usually after a press the of the 'Show Table' button the second time.
Running on the device shows a different behavior. The table and cell are not collected right away. The simplest explanation is that the GC is 'tuned' to only run when it needs to. So if your app is not using much memory, all the objects will continue to live.
There are two things you can do to force the GC to run as shown in the sample.
showTable.TouchUpInside += delegate {
navController.PushViewController (new MyViewController (), true);
// Not a great idea to call Collect but you could to force it.
// GC.Collect ();
};
allocate.TouchUpInside += delegate {
// Trigger the GC by creating a bunch of objects
System.Collections.Generic.List<object> list = new System.Collections.Generic.List <object> ();
for (int i=0; i<2048; i++)
{
list.Add (new object ());
}
};
First you can call GC.Collect. However I do not recommend that. The GC will run best when you let it run when it wants to. (In most cases.) When is it acceptable to call GC.Collect?
Second just keep writing code and let the GC decide what is best. In the sample I added another button that allocates a bunch of objects and adds them to a list. So if you toggle between the table view and the main view a few times then press the allocate button a couple of times you should see the finalizers run.
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using System.Drawing;
namespace delete20130320
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
var mainView = new UIViewController ();
var showTable = UIButton.FromType (UIButtonType.RoundedRect);
showTable.Frame = new System.Drawing.RectangleF (10, 10, 150, 35);
showTable.SetTitle ("Show Table", UIControlState.Normal);
var allocate = UIButton.FromType (UIButtonType.RoundedRect);
allocate.Frame = new System.Drawing.RectangleF (10, 55, 150, 35);
allocate.SetTitle ("Allocate", UIControlState.Normal);
mainView.View.BackgroundColor = UIColor.White;
mainView.View.Add (showTable);
mainView.View.Add (allocate);
var navController = new UINavigationController (mainView);
showTable.TouchUpInside += delegate {
navController.PushViewController (new MyViewController (), true);
// Not a great idea to call Collect but you could to force it.
// GC.Collect ();
};
allocate.TouchUpInside += delegate {
// Trigger the GC by creating a bunch of objects
System.Collections.Generic.List<object> list = new System.Collections.Generic.List <object> ();
for (int i=0; i<2048; i++)
{
list.Add (new object ());
}
};
window.RootViewController = navController;
window.MakeKeyAndVisible ();
return true;
}
}
public class MyViewController : UIViewController
{
UITableView _tableView;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
_tableView = new UITableView (this.View.Bounds);
View.Add (_tableView);
_tableView.DataSource = new MyDataSource ();
}
~MyViewController ()
{
// Bad practice to call other managed objects in finalizer
// But for sample purposes it will be ok
Console.WriteLine ("~MyViewController");
}
protected override void Dispose (bool disposing)
{
// Bad practice to call other managed objects in Dispose
// But for sample purposes it will be ok
Console.WriteLine ("MyViewController.Dispose");
base.Dispose (disposing);
}
class MyDataSource : UITableViewDataSource
{
public override int RowsInSection (UITableView tableView, int section)
{
return 1;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell ("SomeUniqueString");
if (cell != null)
return cell;
return new TableViewWaitCell (UITableViewCellStyle.Default, "SomeUniqueString");
}
}
}
public class TableViewWaitCell : UITableViewCell
{
public TableViewWaitCell(UITableViewCellStyle style, string reuseIdentifier) : base (style, reuseIdentifier)
{
this.SelectionStyle = UITableViewCellSelectionStyle.None;
this.TextLabel.Text = "Something";
}
~TableViewWaitCell()
{
// Bad practice to call other managed objects in finalizer
// But for sample purposes it will be ok
System.Console.WriteLine("TableViewWaitCell.~TableViewWaitCell");
// Avoid forcing the GC
//System.GC.Collect();
}
protected override void Dispose (bool disposing)
{
// Bad practice to call other managed objects in Dispose
// But for sample purposes it will be ok
System.Console.WriteLine("TableViewWaitCell.Dispose");
base.Dispose (disposing);
//GC.Collect();
}
}
}
The problem came from an EventHandler which referenced a method of my view controller thus prenventing the collection of my cells and my controller.

Resources