Recently I was working on a Xamarin.Forms app that required push notifications for both iOS and Android. I started implementing Azure Notification Hub and was recommended by my friend James Montemagno that App Center supports push notifications. James quickly produced an amazing blog post that documents push notification and your options in Xamarin.
This will be a 3-Part Blog series on Push Notifications with Visual Studio App Center
Title | Description |
---|---|
Push Notifications with App Center | The Basics and Configuration |
Sending Push Notifications with the App Center API | How to trigger a Push Notification from code using the App Center API |
Local Notifications with App Center | How to trigger local notifications from a push notification |
The Push Notification Problem
We are building a mobile app and we want to send push notifications, but each platform we are building uses a different push notificaiton server
Platform | Service |
---|---|
iOS | Apple Push Notification Service |
Android | Firebase Cloud Messaging |
Windows | Windows Notification Service |
Suppose we had a simple service we can reference in our project that handles sending notifications out to our devices instead of having to write custom platform specific code potentially 3 times over.
Enter App Center
Microsoft’s App Center now provides support for handling Push Notifications for your mobile app. They even build out some awesome libraries for you to use in your Xamarin.Forms application. Now we can handle 90% of the work we want for Push Notifications in our shared code.
Configure Device Services
Before you go any further you will need to configure your platform specific notification services. Head over to the docs and get your platform specific services configured and then come back
Configuring App Center
Just like most App Center integrations they have provided awesome detailed steps to configuring everything right in the App Center portal
iOS Specific
Configuring iOS is really as simple as copying over the details from the APN certificate that you have created. They even provide documentation on this screen on how to do thata and approprate links.
Head over to visual studio and update your Entitlements.plist
- Allow Push Notifications
That’s it! Yes you read that correctly there is nothing else we need to do to configure iOS
Android Specific
Now that iOS is taken care of you will want to configure your Android Push notification which is typically in a separate App Center App. Head over there and open up the Push Notification Wizard.
The SDK screen is almost identical between the 2 platforms but there is well documented differences, just be sure to read everything on the SDK configuration screen.
Log into Firebase and get the following properties
- Sender ID
- Server Key
Head over to visual studio and update your AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
<!--?xml version="1.0" encoding="utf-8"?-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.hoeflingsoftware.PushSample" android:installLocation="auto">
<uses-sdk android:minSdkVersion="15" />
<!-- app center push - START -->
<permission android:protectionLevel="signature" android:name="${applicationId}.permission.C2D_MESSAGE" />
<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />
<!-- app center push - END -->
<application android:label="PushSample" android:icon="@drawable/icon"></application>
</manifest>
Shared Code Xamarin.Forms
Yes, we are done configuring App Center! We are ready to start receiving push notifications.
Add the following code before you invoke AppCenter.Start()
1
2
3
4
5
6
7
8
9
10
if (!AppCenter.Configured)
{
Push.PushNotificationReceived += (sender, e) =>
{
// handle notification code
Debug.WriteLine("Notification Received!!!");
Debug.WriteLine($"Title: {e.Title}");
Debug.WriteLine($"Message: {e.Message}");
}
}
Test Push Notification
Now everything is setup, let’s head back to the App Center Push Notification Portal and fire a test off.
Before you send the notification you may want to head back to visual studio and setup some breakpoints with the app running.
When testing push notifications it is best to do this on a physical device, you may run into issues on the emulators
- Find the blue button that says “Send notification”
- Fill out the notification Wizard
- Select your devices
- Send Notification
You should receive the notification in your shared code and you can now process it however you want.
Some things you can do with the notification
- Display local notification
- Run some code
- Update data on the screen
- Whatever you want
Now that we know our app is receiving the push notification we can test it while the app is not running. The real use-case for push notifications. Follow these steps prior to testing the push notification, if you do not follow these steps you will run into false negatives where you won’t receive the notification because the debugger is still holding onto the app.
- Deploy your app via visual studio debugger
- Stop the debugger
- Terminate the app
- Launch the app via device
- shutdown the app
At this point you are ready to receive some notifications. Go back to App Center and trigger the notifications for both iOS and Android and you should see notifications like these:
Device Specific Notification
You have a few options from App Center when creating notifications
Type | Description |
---|---|
All Registered Devices | Sends a notification to everyone with your app |
Custom Device List | Sends a notification to a specific list you specify |
Audience | Sends a notification to an App Center customized audience, you are allowed 5 for free |
Code Sample
I have put together a simple Xamarin.Forms app that supports Push Notification. To get this working locally you will need to fork the repository and update the App Center configuration to use your configuration. At that point you should be able to use Push Notifications with App Center.
App Center API
Next time we will talk about using the App Center API and how to send customized push notifications from code. While you wait for the next article take a look at the api
- AppCenter Swagger Api - look for the “push” section