Getting Started
Push notifications come in so many flavours these days. The community needed one way of doing things, but the ability to swap providers in and out at will. The reason, push providers, aside from native have been dropping like flies the last several years. The latest to go was AppCenter which it turns out the Xamarin community was heavily invested in.
Native push is the root of all other push providers. It works at the native OS level and is usually feeding other providers like Azure Notification Hubs and Firebase. As such, the setup instructions found within this document generally apply to all.
Setup
General OS setup is mostly the same on all platforms, but please review the specific provider you intend to use from the menu for more information
Registration
Look to each appropriate provider to see setups for each. The most important function otherwise, is RequestAccess shown below which will give you the push notification token that you can send to your backend.
All providers use the native implementations on the platform to some degree, as such, you will always need to call
using System;
using System.Threading.Tasks;
using Shiny;
using Shiny.Push;
public class PushRegistration
{
public async Task CheckPermission()
{
var push = ShinyHost.Resolve<IPushManager>();
var result = await push.RequestAccess();
if (result.Status == AccessState.Available)
{
// good to go
// you should send this to your server with a userId attached if you want to do custom work
var value = result.RegistrationToken;
}
}
}
Background Delegate
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Shiny.Push;
public class PushDelegate : IPushDelegate
{
public async Task OnEntry(PushEntryArgs args)
{
// fires when the user taps on a push notification
}
public async Task OnReceived(IDictionary<string, string> data)
{
// fires when a push notification is received (silient or notification)
}
public async Task OnTokenChanged(string token)
{
// fires when a push notification change is set by the operating system or provider
}
}
Foreground Monitoring
It is quite often that you may want to change data due to a silent notification being received. This is similar to watching a SignalR broadcast, but with observables because RX is awesome and Shiny dies on the RX hill!
using System.Reactive.Linq;
using Shiny;
using Shiny.Push;
public class PushForeground
{
public void YourMethod()
{
var push = ShinyHost.Resolve<IPushManager>(); // assign through DI, static, or ShinyHost.Resolve
var disp = push
.WhenReceived()
.Where(x => x["newdata"] == "true")
.SubscribeAsync(async data =>
{
// make you HTTP call here
});
}
}
Additional Features
Like other modules in Shiny, there are certain providers that support additional feature sets. Push really only has 1 extra, tagging.
The following providers, support tagging
- Azure Notification Hubs
- Firebase
In order to safely support tagging without the need for constantly feature flag or type checking, the following extension methods exist to make life easy
using System.Threading.Tasks;
using Shiny;
using Shiny.Push;
public class Extensions
{
public async Task Method()
{
var push = ShinyHost.Resolve<IPushManager>();
var supported = push.IsTagsSupport();
// tries to set a params list of tags if available
await push.TrySetTags("tag1", "tag2");
// gets a list of currently set tags
var tags = push.TryGetTags();
// requests permission from the user and sets tags if available
var permissionResult = await push.TryRequestAccessWithTags("tag1", "tag2");
}
}
Android
Follow the exact same setup process as the native push provider
Please read the following Microsoft Document on how to setup firebase within your Android application.
The process of setting up Android is a bit of process
<ItemGroup>
<GoogleServicesJson Include="google-services.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</GoogleServicesJson>
<PackageReference Include="Xamarin.GooglePlayServices.Basement" Version="The same version Shiny.Push is using" />
<PackageReference Include="Xamarin.GooglePlayServices.Tasks" Version="The same version Shiny.Push is using" />
</ItemGroup>
iOS
Follow the exact same setup process as the native push provider with one addition
After setting up your iOS app within the firebase admin portal, add GoogleService-Info.plist to your iOS head project and mark it as a "BundleResource"
Frequently Asked Questions
Q: The delegate method 'OnReceived' is not firing on Android
A: You must include data payload in your push notification
Q: The delegate method 'OnReceived' is not firing on iOS
A: You must set content-available: 1 in your push notification
Q: I've been sending a push every few minutes and iOS stops firing the OnReceived delegate method
A: In most production settings, iOS gives you about 3-5 silent push notifications per hour before Apple starts throttling/ignorning them.