Thank You Liv

After almost a year hard work Liv has reached her goal and been accepted to the Edmonton Police Service (EPS).  Needless to say she was a bit excited when she got the call.

Unfortunately joining the EPS means Liv no longer has time to work with Saturday MP.  At least in the immediate future.  That means there will be a drought of cartoons and a decrees in qualty qulity quaity quality of the blog and social media posts.  We are as sad as you are but also happy Liv was able to accomplish her goal.

Edmonton Police Service Logo

Everyone should admire Liv’s dedication and hard work.  She never gave up.  We are very proud of her accomplishment and wish her all the best in her new career.  Hopefully our paths will cross again in the future.

Posted in Business Side | Tagged , | Comments Off on Thank You Liv

Do you Still Need to Compress Pictures?

Mini-Compressor LogoI hope you had a great summer vacation.  We sure did and are trying squeeze in a mini-vacation before the summer is officially over after the Labour Day long weekend.

Now that your summer vacation is over you want to share your pictures with friends, family, subscribers, and strangers on the Internet.  How do you do that?

Today you just login to Facebook, Flickr, Snapchat, iCloud, Google Photos (formally Picasa), Shutterfly, etc, and upload your pictures.  If you took the pictures with your phone your picture might have been auto-magically uploaded to iCloud or Google.  You don’t need to worry about the size of the picture or how much memory it consumes.  Often the service will compress your picture on the fly as needed.

It’s even easy to send pictures directly to one or a couple people by attaching the photos to an e-mail or text message on your phone.  The size limits for both e-mail and text attachments is very generous.  For example GMail allows 25MB attachments.  No need to compress, at least most of the time.

Why am I rambling about image compression?  Because Saturday MP sells Mini-Compressor, a program to compresses images.  Is Mini-Compressor still relevant today?  Is it worth my time and money to continue to support and sell it?

If you still use Mini-Compressor please let us know.  If you don’t please also let us know.  You can let us know via e-mail, Facebook, or Twitter.  Haven’t heard of Mini-Compressor before and don’t know if you need it then try it for free (click the Try Now button).

I’m also curious if people would be more comfortable buying Mini-Compressor on the Windows Store instead of the Saturday MP website.  If I did release Mini-Compressor on the Windows Store it would lose the right click feature (link with eye glazing technical details).  I always thought that was one of the best features of Mini-Compressor but maybe I’m wrong.

If you are a current user of Mini-Compressor don’t worry it won’t disappear.  In the worst case, I’ll open source it and make sure the existing installers and executables are freely available.  Finally thank you for using Mini-Compressor, I hope it made you life better in some small way.

Posted in Business Side, Mini-Compressor | Tagged , | Comments Off on Do you Still Need to Compress Pictures?

Looking for Speakers for the Edmonton .NET User Group

Edmonton .NET User Group LogoAre you passionate about software development and want to share your passion with others?  If so then the Edmonton .NET Users Group is looking for you. Specifically as the newly elected Program Director I’m looking for you to speak at one of our meetups.

Already a member?  That’s even better.  Don’t hold back, share with the group.

You don’t have be a world-renowned speaker.  In fact, the .NET Users Group is a friendly and supportive environment for first time speakers.  The biggest thing we ask for is passion and willingness to share your knowledge.

Talks can be about anything you like as long it is related to software development in some form.  It also helps if the talk is related to .NET or Windows.  If you, or someone you know, is interested in presenting please let me know via e-mail or contact me via Meetup (click on my picture).

P.S. – Not interested in speaking but want to connect with passionate, knowledgeable developers?  Join the Edmonton .NET User Group or your friendly local development group.

Posted in Business Side | Tagged | Comments Off on Looking for Speakers for the Edmonton .NET User Group

Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 4 The Actual Binding

This is the 4th and final part about incorporating an objective-c library in Xamarin.  In my case I’m binding BEMCheckBox.  In part 3 we used Sharpie to create a C# interface for the BEMCheckBox.  In this post we will show how to use that interface. Finally you can find a working example of BEMCheckBox in Xamarin iOS here.

First take the APIDefinitions.cs and Structs.cs files in a location accessible to Visual Studio.  Also take the BEMCheckBox.framework folder and move this location accessible to Visual Studio.  For me this means moving the files to my Windows computer.

Now open up the Xamarin project that will consume the objective-c library and add a new project.  When prompted for the type pick Bindings Library (iOS).

Add Binding Library

Next add a reference to the BEMCheckBox framework.  Use the special native reference just as you would a normal reference.

Add Native Reference
Add BEMCheckBox Framework
Native Reference Added

Next replace the ApiDefinition and Structs files with the ones we generated using Sharpie.  First up the the ApiDefinition which originally looked like:

namespace BEMCheckBoxBinding
{
  // The first step to creating a binding is to add your native library ("libNativeLibrary.a")
  // to the project by right-clicking (or Control-clicking) the folder containing this source
  // file and clicking "Add files..." and then simply select the native library (or libraries)
  // that you want to bind.

  // More comments....
}

Now it should look like:

namespace BEMCheckBoxBinding
{
  // @interface BEMCheckBoxGroup : NSObject
  [BaseType (typeof(NSObject))]
  interface BEMCheckBoxGroup
  {
    // @property (readonly, nonatomic, strong) NSHashTable * _Nonnull checkBoxes;
    [Export ("checkBoxes", ArgumentSemantic.Strong)]
    NSHashTable CheckBoxes { get; }

    // @property (nonatomic, strong) BEMCheckBox * _Nullable selectedCheckBox;
    [NullAllowed, Export ("selectedCheckBox", ArgumentSemantic.Strong)]
    BEMCheckBox SelectedCheckBox { get; set; }

    // More properties and methods...
  }

  // @interface BEMCheckBox : UIControl <CAAnimationDelegate>
  [BaseType (typeof(UIControl))]
  interface BEMCheckBox : ICAAnimationDelegate
  {
    [Wrap ("WeakDelegate")]
    [NullAllowed]
    BEMCheckBoxDelegate Delegate { get; set; }

    // @property (nonatomic, weak) id<BEMCheckBoxDelegate> _Nullable delegate __attribute__((iboutlet));
    [NullAllowed, Export ("delegate", ArgumentSemantic.Weak)]
    NSObject WeakDelegate { get; set; }

    // @property (nonatomic) BOOL on;
    [Export ("on")]
    bool On { get; set; }

    // More properties and methods...
  }

  // @protocol BEMCheckBoxDelegate <NSObject>
  [Protocol, Model]
  [BaseType (typeof(NSObject))]
  interface BEMCheckBoxDelegate
  {
    // @optional -(void)didTapCheckBox:(BEMCheckBox * _Nonnull)checkBox;
    [Export ("didTapCheckBox:")]
    void DidTapCheckBox (BEMCheckBox checkBox);

    // @optional -(void)animationDidStopForCheckBox:(BEMCheckBox * _Nonnull)checkBox;
    [Export ("animationDidStopForCheckBox:")]
    void AnimationDidStopForCheckBox (BEMCheckBox checkBox);
  }
}

The structs file originally looked like:

namespace BEMCheckBoxBinding
{
}

Now it should look like:

namespace BEMCheckBoxBinding
{
  [Native]
  public enum BEMBoxType : nint
  {
    Circle,
    Square
  }

  [Native]
  public enum BEMAnimationType : nint
  {
    Stroke,
    Fill,
    Bounce,
    Flat,
    OneStroke,
    Fade
  }
}

Now try to compile and in my case I got a couple of errors.  Depending on what Sharpie generated, or if you did manually, you might get different errors.  In my case I got the following errors:

Type byte, sbyte, short, ushort, int, uint, long or ulong expected

The type or namespace name `NSHashTable' could not be found. Are you missing an assembly reference?

The type or namespace name `ICAAnimationDelegate' could not be found. Are you missing an assembly reference? NativeLibrary1

The first error is in the structs file.  Just delete the nint so it looks like:

namespace BEMCheckBoxBinding
{
  [Native]
  public enum BEMBoxType
  {
    Circle,
    Square
  }

  [Native]
  public enum BEMAnimationType
  {
    Stroke,
    Fill,
    Bounce,
    Flat,
    OneStroke,
    Fade
  }
}

The second error occurs because there is no NSHashTable in Xamarin.  In this case I just deleted the method.  I’ll have to figure out a better solution in the future.

namespace BEMCheckBoxBinding
{
  // @interface BEMCheckBoxGroup : NSObject
  [BaseType (typeof(NSObject))]
  interface BEMCheckBoxGroup
  {
    // Delete this method.
    // @property (readonly, nonatomic, strong) NSHashTable * _Nonnull checkBoxes;
    //[Export ("checkBoxes", ArgumentSemantic.Strong)]
    //NSHashTable CheckBoxes { get; }

    // @property (nonatomic, strong) BEMCheckBox * _Nullable selectedCheckBox;
    [NullAllowed, Export ("selectedCheckBox", ArgumentSemantic.Strong)]
    BEMCheckBox SelectedCheckBox { get; set; }

    // More properties and methods...
  }
}

Finally to fix the last error just remove the ICAAnimationDelegate inheritance.  In this case we don’t need it.  At least I don’t think we do.  The checkboxes seem to work fine without it.

namespace BEMCheckBoxBinding
{
  // @interface BEMCheckBox : UIControl <CAAnimationDelegate>
  [BaseType(typeof(UIControl))]
  interface BEMCheckBox
  {
    [Wrap("WeakDelegate")]
    [NullAllowed]
    BEMCheckBoxDelegate Delegate { get; set; }

    // @property (nonatomic, weak) id<BEMCheckBoxDelegate> _Nullable delegate __attribute__((iboutlet));
    [NullAllowed, Export("delegate", ArgumentSemantic.Weak)]
    NSObject WeakDelegate { get; set; }

    // More properties and methods...
  }
}

Now everything should compile.  When the compile succeeds you should find a couple generated files hidden in the obj/Debug/ios/BEMCheckBoxBinding folder.  In this case there are 3 files, one for each interface: BEMCheckBox.g.cs, BEMCheckBoxDelegate.g.cs, BEMCheckBoxGroup.g.cs.

What the Native Binding project does is parse the interface your created and creates an actual class.  The methods are all pass through methods to the underlying objective-c framework and look something like:

[Export ("reload")]
[CompilerGenerated]
public virtual void Reload ()
{
  if (IsDirectBinding) {
    global::ApiDefinition.Messaging.void_objc_msgSend (this.Handle, Selector.GetHandle ("reload"));
  } else {
    global::ApiDefinition.Messaging.void_objc_msgSendSuper (this.SuperHandle, Selector.GetHandle ("reload"));
  }
}

The last thing to do is actually use the new method.  From a iOS project add a reference to the BEMCheckBoxBinding project.  Then create a checkbox as so:

public override void ViewDidLoad()
{
  base.ViewDidLoad();

  Title = "Binding Example";
  var bemCheckBoxLabel = new UILabel()
  {
    Text = "BEMCheckBox:",
    Frame = new CoreGraphics.CGRect(10, 40, 125, 25)
  };

  var checkbox = new BEMCheckBoxBinding.BEMCheckBox(new CoreGraphics.CGRect(140, 40, 25, 25));

  View.AddSubview(bemCheckBoxLabel);
  View.AddSubview(checkbox);
}

And your checkbox should appear.

BEMCheckBox Working Example

You have now successfully bound and objective-c library to and Xamarin project.  Get yourself a couple cookies.  Like a whole box.  Eat some and share them with your friends.

P.S. – This is my favourite cookie monster skit of all time.  No cookie, no guessing game, arrivederci frog.

Save

Posted in Code Examples, Software Development, Today I Learned | Tagged , , , , , | Comments Off on Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 4 The Actual Binding

Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 3 Using Sharpie to Create Binding Interface

This is the 3rd part about using the BEMCheckBox, an Objective-C library, in Xamarin.  Part 1 described how to compile the BEMCheckBox library using Xcode.  Part 2 showed how to combine the multiple libraries we compiled from Part 1 into one library.  In this part I’ll describe how to use Sharpie to create the C# API interface.

Sharpie is a tool that reads the header files in the BEMCheckBox framework and extract a C# interface.  We could create the interface manually but it’s easier if we let technology do some of the work for us.  Don’t worry about this job being replaced by a robot as we will have to manually edit the file after it’s generated, at least for BEMCheckBox.  Download the sharpie tool here and read more about the sharpie here.

Remember this screen shot from Part 2 where we merged two libraries into one?

Lipo Command Screen Shot

This is the library we created that supports multiple architectures (x86, x64, Arm7, Arm64).  We need to run Sharpie on this the header files in this single framework.  Open up the terminal and run this command to see what sdk versions are installed.

sharpie xcode -sdks

This will show something like:

sdk: appletvos10.2    arch: arm64
sdk: iphoneos10.3     arch: arm64  arm7
sdk: macosx10.12      arch: x86_64 i386
sdk: watchos3.2       arch: arm7k

Once you know the iphone sdk version you can run the actual sharpie command.  Run this command against the header files in the BEMCheckBox framework folder.

sharpie bind -sdk iphoneos10.3 BEMCheckBox.framework/Headers/BEMCheckBox.h

The output should look something like:

Parsing 1 header files...

Binding...
  [write] ApiDefinitions.cs
  [write] StructsAndEnums.ca

Submitting usage data to Xamarin...
  Submitted - thank you for helping to improve Objective Sharpie!

Done.

A screen shot of the commands is below.

Sharpie Command

This should generate two files for BEMCheckBox: ApiDefinitions.cs and StructsAndEnums.cs.  For other libraries you might only get one file.

Go ahead and look at the files now.  In the APIDefinitions file you will C# interfaces with a bunch of attributes.  The interface maps the methods in the objective-c header file.  In the StructsAndEnums file you will see enums used by BEMCheckBox.  A snippet of the files is shown below.

API Definitions:

// @interface BEMCheckBox : UIControl <CAAnimationDelegate>
[BaseType(typeof(UIControl), Delegates = new string[] { "WeakDelegate" }, Events = new Type[] { typeof(BEMCheckBoxDelegate) })]
interface BEMCheckBox
{
  [Export("initWithFrame:")]
  IntPtr Constructor(CGRect frame);

  [Wrap("WeakDelegate")]
  [NullAllowed]
  BEMCheckBoxDelegate Delegate { get; set; }

  // @property (nonatomic, weak) id<BEMCheckBoxDelegate> _Nullable delegate __attribute__((iboutlet));
  [NullAllowed, Export("delegate", ArgumentSemantic.Assign)]
  NSObject WeakDelegate { get; set; }

  // @property (nonatomic) BOOL on;
  [Export("on")]
  bool On { get; set; }

  // @property (nonatomic) CGFloat lineWidth;
  [Export("lineWidth")]
  nfloat LineWidth { get; set; }

  // Other methods...

}

Structs:

[Native]
public enum BEMBoxType : long
{
  Circle,
  Square
}

[Native]
public enum BEMAnimationType : long
{
  Stroke,
  Fill,
  Bounce,
  Flat,
  OneStroke,
  Fade
}

This is all well and good but an interface can’t make calls to the underlying objective-c interface.  I’ll discuss how Visual Studio auto-magically creates the wiring between the C# interface and the BEMCheckBox library in Part 4.  For now go ahead and get another cookie.  You deserve it.

Don’t forget you can find a working example of BEMCheckBox in Xamarin iOS here.

 

P.S. – A classic Cookie Monster song about up and down.  Can you figure out what happened to Cookie Monster’s cookie before the end of the song?

Save

Posted in Code Examples, Software Development, Today I Learned | Tagged , , , , , , | Comments Off on Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 3 Using Sharpie to Create Binding Interface

Kids Thinking Outside the Box – Favourite Cheese

Transcript of my wife talking to my daughter’s grade 6 friend:

Wife: “What is your favourite cheese?”

Friend: “Grated!”

Posted in Fun, Kids Thinking Outside the Box | Tagged | Comments Off on Kids Thinking Outside the Box – Favourite Cheese

Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 2 Combining Libraries

This is part 2 of creating a iOS Binding for the Objective-C Library BEMCheckBox.  In part 1 we compiled the Xcode project into two separate libraries.  One for iphones (ARM architecture) and one for iOS simulators (x86/x64 architecture).

In this post I’ll discuss how to combine two libraries into one that contains all the architectures.  If you fail to combine the libraries then you will get errors when you bind to Xamarin.  If you only have the iphone library then it won’t run on the simulator and vice versa.

Just a reminder that you should have the Release-iphoneos and Release-iphonesimulator builds as shown below.

Compiled Frameworks In Finder

To combine the libraries requires the use of the lipo tool.  Before we lipo these libraries we first need to get setup.  To start with copy the Release-iphoneos/BEMCheckBox.framework folder to the Products folder as shown below.

Copy BEMCheckBox Framework

Results Of Copying BEMCheckBox Framework

It actually doesn’t matter if you copy the iphone or simulator folder.  Inside that folder is the compiled library along with some other such as uncompiled header files.  The library file can be found at BEMCheckBox.framework/BEMCheckbox.  This file will be different between the iphone and simulator releases but the header files will be the same.

Our goal is to replace the BEMCheckbox compiled library that only supports ARM with one that supports all the architectures.  This is where we use lipo.  Open up a terminal window on your Mac and navigate to the Products folder.  Then enter the following command:

lipo -create -output BEMCheckBox.framework/BEMCheckBox Release-iphoneos/BEMCheckBox.framework/BEMCheckBox Release-iphonesimulator/BEMCheckBox.framework/BEMCheckbox

This will combine the iphone and simulator libraries into one library and replace the one we in the products folder with the combined one.  To make sure everything worked as expected run the file command as shown below.

file BEMCheckBox.framework/BEMCheckBox

You should get the following output or something similar:

BEMCheckBox.framework/BEMCheckBox: Mach-0 universal binary with 4 architectures: [i386: Mach-0 dynamically lined shared library i386]&amp;nbsp;[x86_x64: Mach-0 dynamically lined shared library x86_x64]&amp;nbsp;[arm_v7: Mach-0 dynamically lined shared library arm_v7]&amp;nbsp;[arm64: Mach-0 dynamically lined shared library arm64]
BEMCheckBox.framework/BEMCheckBox (for architecture i386): Mach-0 dynamically linked shared library i386
BEMCheckBox.framework/BEMCheckBox (for architecture x86_x64): Mach-0 dynamically linked shared library x86_x64
BEMCheckBox.framework/BEMCheckBox (for architecture arm_v7): Mach-0 dynamically linked shared library arm_v7
BEMCheckBox.framework/BEMCheckBox (for architecture arm64): Mach-0 dynamically linked shared library arm64

You can also use the lipo command tool to check the library.

lipo -info BEMCheckBox.framework/BEMCheckBox

You should see something like:

Architectures in the fat file: BEMCheckBox.framework/BEMCheckBox are: i386, x86_x64, arm7, arm64

A screen show of all the commands looks like:

Lipo Command Screen Shot

We now have one library that will work with Xamarin and both iOS simulators and physical iPhones.  Like combining a chocolate and peanut butter cookie.  In Part 3 I’ll explain how to create the binding between this delicious chocolate and peanut butter cookie and Xamarin.  Finally you can find a working example of BEMCheckBox in Xamarin iOS here.

 

P.S. – Not a song this time but a cookie monster skit that makes me laugh.

Posted in Code Examples, Software Development, Today I Learned | Tagged , , , , | Comments Off on Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 2 Combining Libraries

Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 1 Compiling the Objective-C Library

There is no checkbox for Xamarin Forms applications.  This is a problem because I wanted a checkbox in a application I’m working on.  A switch wasn’t going to cut it.

After a bit of research I found that Android had native checkbox.  Half my problem was solved.  I celebrated by eating a cookie.

Then I ran into a problem because iOS does not have a native checkbox control.  I found others had solved this problem by creating a iOS button and adding a checkbox image to it.  One image for checked and one for unchecked.  This works but it means I can’t easily change the colour, size, etc. of the checkbox.  Any changes would require me to create new images.  Using MS Paint or GIMP is not my strong suit.

Then I found BEMCheckBox which does everything I need it to.  It also allows for circle and square checkboxes.  But wait, BEMCheckBox is in Objective-C.  How does I reference it in my Xamarin project?  Native bindings to the rescue!

Step one is compile the native library using Xcode.  Download the code and open it in Xcode.  Then make sure the project is being built for release.  If you are like me and not very familiar with Xcode the exact steps are:

  1. Click the BEMCheckBox project and choose edit schema.  In the dialog pick Run on the left and Info in the middle.
  2. Then change the build configuration to Release.

Next we need to build the library.  Actually we need to build it twice, once for the iOS simulators (x86 and x64 architectures) and once for physical iOS devices (ARM architectures).  To build for a iOS device make sure the area beside the BEMCheckBox says Generic iOS Device.  Then pick Product->Build For->Running from the menu.

Build For iOS Device

Then switch the Generic iOS Device to any iOS simulator and do the same thing.

Build For iOS Simulator

To find the two builds you just created right click on the BEMCheckBox.framework in the Products folder and choose Show in Finder.

Show Compiled Framework In Finder

You should see both a Release-iphoneos and Release-iphonesimulator folders.   You might also see Debug-<os> folders but you can ignore those.

Compiled Frameworks In Finder

That is it for part 1.  Good work, help yourself to a cookie.  In part 2 we will combine the two libraries into one.  If you don’t combine the builds then the library will only work on the simulators or physical devices in Xamarin not both.  Obviously we want the framework to work with both.  Finally you can find a working example of BEMCheckBox in Xamarin iOS here.

 

P.S. – I really like cookies.  They are my favourite desert dessert.

P.S.S. – Why is desert and dessert so similar?

Chocolate pudding
It no phase me
But you got cookie
So share it maybe

Save

Posted in Code Examples, Software Development, Today I Learned | Tagged , , , , | Comments Off on Today I Learned How to Create a Xamarin iOS Binding for Objective-C Libraries – Part 1 Compiling the Objective-C Library

You Can’t Just Turn Creativity On Like A Faucet

Lately, Chris has been working on Xamarin notifications in Android, which you would have gathered from his recent blog posts. I have been tasked with designing an icon to go with this work. I’ve designed a few things before (Scary Sun being my best work), but this task seemed to be especially vexing.

How would I come up with something that captures the concepts of Xamarin, Saturday Morning Productions,  and notifications? It seemed like a lot to fit into a very small space. Now, when I think of being notified for something, I immediately think of bells. Like alarm clock bells. I drew a few iterations of an icon with a bell, but honestly, they looked kinda dumb. I’m not sure anyone would look at them and connect the three things together that I just mentioned. I wondered if I would get a flash of inspiration if I just let it percolate around my brain a bit.

The problem with that approach is that time becomes a very loose concept. Before I knew it, a whole week had passed. Tricky things, those weeks. I had to send something to the team! I started at a blank piece of paper, hoping creativity would find me.

I looked at the Xamarin logo again. You know, I could probably make that shape into an alarm clock…I thought. So I drew some legs and bells on the Xamarin logo and added some music notes. You know, to imply sound. But how to fit in the SMP reference? After a chat with Ada and Chris, they suggested turning “SMP” into musical notes themselves. It’s just crazy enough that it might work, I mused. They were right. Even thought it wasn’t my proudest moment for leaving something to the last minute, I am pleased with the results.

And, just for good measure, here is the Calvin & Hobbes post that inspired this post!

(Source: Calvin & Hobbes. These days Calvin and Hobbes seem to sum up most of my concepts!)

Posted in Fun, Uncategorized | Comments Off on You Can’t Just Turn Creativity On Like A Faucet

Today I Learned How to Handle Local Notifications in Xamarin on Android

In my previous example I detailed how to schedule notifications in Xamarin on Android but didn’t show the Xamarin Forms application reacting to that specific notification.  Let’s fix that.

First override the OnNewIntent method in the MainActivity.  We override this method so notifications will work if the application is currently running or is stopped and the notification started the application.  In the OnNewIntent method you put code that makes sure it’s a notification you want to respond to then extracts any needed information from the notification.  An example is below:

protected override void OnNewIntent(Intent intent)
{
  // Let the parent do it's thing.
  base.OnNewIntent(intent);

  // Is this a notification intent? The action should look like:
  //
  // &amp;amp;lt;packagename&amp;amp;gt;.NOTIFICATION-&amp;amp;lt;ID&amp;amp;gt;
  //
  var expectedActionName = BuildActionName("");
  if (intent.Action == null || !intent.Action.StartsWith(expectedActionName))
  {
    // Not a notification intent. Do nothing.
    return;
  }

  // Extract the notification information.
  var extractedNotification = new Notification
  {
    Id = intent.Action.Substring(intent.Action.IndexOf("-", StringComparison.Ordinal) + 1), // The ID is part of the action. 
    Title = intent.GetStringExtra(TitleExtrasKey), // These fields are extras but should exist.
    Message = intent.GetStringExtra(MessageExtrasKey) // These fields are extras but should exist.
  };

  // Do whatever you want with the notification.
  NotificationRecieved(extractedNotification);
}

The first thing the method does is check if it’s a notification we are interested in.  In this example we know that notifications we are interested in have a certain action name so we check for that.

var expectedActionName = BuildActionName("");
if (intent.Action == null || !intent.Action.StartsWith(expectedActionName))
{
  // Not a notification intent. Do nothing.
  return;
}

Next we extract the notification information.  In this case we only pull out the ID, title, and message but you could add other things as needed.

var extractedNotification = new Notification
{
 Id = intent.Action.Substring(intent.Action.IndexOf("-", StringComparison.Ordinal) + 1), // The ID is part of the action. 
 Title = intent.GetStringExtra(TitleExtrasKey), // These fields are extras but should exist.
 Message = intent.GetStringExtra(MessageExtrasKey) // These fields are extras but should exist.
};

Finally do whatever you want with this the notification.  This could be showing a certain screen in your application, updating some data, etc.  In this example we just call a handling method and leave it up to your very vivid imagination.

If you want a working example checkout the XPlugins.Notifications plugin.  If you are writing an Xamarin Forms application you can use the plugin to handle notifications in both Android and iOS.

P.S. – Another post about communication and another music video about communication.  The song is not about communication but the video is.  Just watch it and you will figure it out.

P.P.S. – This is the only song I can play on guitar and sing at the same time.  Both poorly.

 

 

 

Posted in Code Examples, Today I Learned | Tagged , , , , | Comments Off on Today I Learned How to Handle Local Notifications in Xamarin on Android