* Update to AndroidX

* Updating Android Xamarin

* Updating samples readme

* Add link to main README to samples
This commit is contained in:
Matthew Podwysocki 2020-11-20 18:22:41 -05:00 коммит произвёл GitHub
Родитель 2a45cc2128
Коммит dd7adb5192
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 262 добавлений и 48 удалений

Просмотреть файл

@ -10,6 +10,8 @@ This repository is set into sections:
- [Xamarin Apple](#getting-started-with-xamarin-for-apple)
- [Xamarin.Android](#getting-started-with-xamarinandroid)
This repository also contains samples which can be found in the [samples](samples/README.md) along with a description of each.
## Getting Started with Xamarin.Forms
This project contains `Microsoft.Azure.NotificationHubs.Client`, which when added to your project, provides a consistent experience across Xamarin Android and Xamarin for Apple. Either download or clone the repository and either add the source from the project to your own solution, or create a NuGet package from the source and add from a local NuGet source.

Просмотреть файл

@ -16,7 +16,7 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>

190
samples/README.md Normal file
Просмотреть файл

@ -0,0 +1,190 @@
# Sample applications for Azure Notification Hubs SDK for Xamarin
This directory contains the sample applications for the following:
- [Xamarin.Forms](NotificationHubSample)
- [Xamarin.iOS](bindings/NHubSampleXamariniOS)
- [Xamarin.Android](bindings/NHubSampleXamarinAndroid)
## Xamarin Forms
The [Xamarin.Forms](NotificationHubSample) uses the `Microsoft.Azure.NotificationHubs.Client` source project to create a cross platform implementation using Azure Notification Hubs. This sample provides support for both iOS and Android. This sample only needs code in the main NotificationHubSample project to start the SDK and listen for messages.
In the `MainPage.xaml` file, you can initialize the Azure Notification Hubs using the followingc code to listen for messages, set up installation lifecycle management, and to initialize the SDK with the Access Policy connection string and hub name.
```csharp
using Microsoft.Azure.NotificationHubs.Client;
public class MainPage
{
public MainPage()
{
InitializeComponent();
// Listen for messages
NotificationHub.NotificationMessageReceived += OnNotificationMessageReceived;
// Listen for installation save updates/failures
NotificationHub.InstallationSaved += OnInstallatedSaved;
NotificationHub.InstallationSaveFailed += OnInstallationSaveFailed;
// Start the SDK
NotificationHub.Start(Constants.ConnectionString, Constants.HubName);
}
}
```
We can then listen for messages using the `OnNotificationMessageReceived` method which has the title, body, and dictionary of the message itself. Here, you can display the message contents.
```csharp
private void OnNotificationMessageReceived(object sender, NotificationMessageReceivedEventArgs e)
{
// Write the message contents
Console.WriteLine(e.Title);
Console.WriteLine(e.Body);
}
```
We can listen for installation saved success or failures which can be helpful for diagnosing issues.
```csharp
private void OnInstallationSaveFailed(object sender, InstallationSaveFailedEventArgs e)
{
Console.WriteLine($"Failed to save installation: {e.Exception.Message}");
}
private void OnInstallatedSaved(object sender, InstallationSavedEventArgs e)
{
Console.WriteLine($"Installation ID: {e.Installation.InstallationId} saved successfully");
}
```
## Xamarin.iOS
We also have a [Xamarin.iOS](bindings/NHubSampleXamariniOS) sample which shows the basic usage of the [Xamarin.Azure.NotificationHubs.iOS](https://www.nuget.org/packages/Xamarin.Azure.NotificationHubs.iOS/) NuGet package which is published through the [Xamarin Components](https://github.com/xamarin/XamarinComponents/tree/master/XPlat/AzureMessaging) repository.
To get started, register for messages in the `AppDelegate.cs` class with the following code to listen for messages, set up installation lifecycle management, and to initialize the SDK with the Access Policy connection string and hub name.
```csharp
using WindowsAzure.Messaging.NotificationHubs;
[Register("AppDelegate")]
public class AppDelegate : UIResponder, IUIApplicationDelegate
{
[Export("application:didFinishLaunchingWithOptions:")]
public bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Listen for messages
MSNotificationHub.SetDelegate(new NotificationDelegate());
// Listen for installation saved success/failure
MSNotificationHub.SetLifecycleDelegate(new InstallationLifecycleDelegate());
// Start the Notification Hub SDK
MSNotificationHub.Start(Constants.ConnectionString, Constants.HubName);
return true;
}
}
```
To listen for messages from APNS, create a class that inherits from `MSNotificationHubDelegate` which has the `DidReceivePushNotification` method.
```csharp
public class NotificationDelegate : MSNotificationHubDelegate
{
public override void DidReceivePushNotification(MSNotificationHub notificationHub, MSNotificationHubMessage message)
{
// Determine whether in foreground or background
if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Background)
{
Console.WriteLine($"Message received in the background with title {message.Title} and body {message.Body}");
}
else
{
Console.WriteLine($"Message received in the foreground with title {message.Title} and body {message.Body}");
}
}
}
```
To listen to lifecycle management such as whether the installation save succeeded or failed on the backend, you can implement a class which inherits from the `MSInstallationLifecycleDelegate` class.
```csharp
public class InstallationLifecycleDelegate : MSInstallationLifecycleDelegate
{
public override void DidFailToSaveInstallation(MSNotificationHub notificationHub, MSInstallation installation, NSError error)
{
Console.WriteLine($"Save installation failed with exception: {error.LocalizedDescription}");
}
public override void DidSaveInstallation(MSNotificationHub notificationHub, MSInstallation installation)
{
Console.WriteLine($"Installation successfully saved with Installation ID: {installation.InstallationId}");
}
}
```
## Xamarin.Android
In addition to Xamarin Forms, we also have a sample using [Xamarin.Android](bindings/NHubSampleXamarinAndroid) which shows the basic usage of the [Xamarin.Azure.NotificationHubs.Android](https://www.nuget.org/packages/Xamarin.Azure.NotificationHubs.Android/) NuGet package which is published through the [Xamarin Components](https://github.com/xamarin/XamarinComponents/tree/master/XPlat/AzureMessaging) repository.
To get started, register for messages in the `MainActivity.cs` class with the following code to listen for messages, set up installation lifecycle management, and to initialize the SDK with the Access Policy connection string and hub name.
```csharp
using WindowsAzure.Messaging.NotificationHubs;
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
// Set listener for receiving messages
NotificationHub.SetListener(new SampleNotificationListener());
// Set listener for installation save success and failure
NotificationHub.SetInstallationSavedListener(new InstallationSavedListener());
NotificationHub.SetInstallationSaveFailureListener(new InstallationSaveFailedListener());
// Initialize with hub name and connection string
NotificationHub.Start(Application, Constants.HubName, Constants.ConnectionString);
}
}
```
To listen for messages from Firebase, create a class that inherits from `INotificationListener` interface which has the `OnPushNotificationReceived` method.
```csharp
public class SampleNotificationListener : Java.Lang.Object, INotificationListener
{
public void OnPushNotificationReceived(Context context, INotificationMessage message)
{
Console.WriteLine($"Message received with title {message.Title} and body {message.Body}");
}
}
```
To listen to lifecycle management such as whether the installation save succeeded or failed on the backend, you can implement classes which inherits from the `IInstallationAdapterListener` interface for successful saves with the `OnInstallationSaved` method which contains the current `Installation`.
```csharp
// Listen for installation saved success
public class InstallationSavedListener : Java.Lang.Object, IInstallationAdapterListener
{
public void OnInstallationSaved(Installation installation)
{
Console.WriteLine($"Installation successfully saved with Installation ID: {installation.InstallationId}");
}
}
```
We can then listen for installation saved failures using the `IInstallationAdapterErrorListener` interface which has the `OnInstallationSaveError` method with the exception.
```csharp
public class InstallationSaveFailedListener : Java.Lang.Object, IInstallationAdapterErrorListener
{
public void OnInstallationSaveError(Java.Lang.Exception javaException)
{
var exception = Throwable.FromException(javaException);
Console.WriteLine($"Save installation failed with exception: {exception.Message}");
}
}
```

Просмотреть файл

@ -18,7 +18,7 @@
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v10.0</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
@ -119,7 +119,7 @@
<PackageReference Include="Xamarin.Android.Support.Media.Compat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.2.1-preview02" />
<PackageReference Include="Xamarin.Android.Volley" Version="1.1.1.1" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.1" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.3" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -71,7 +71,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.iOS" Version="3.1.2" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.iOS" Version="3.1.1" />
</ItemGroup>
<ItemGroup>
<ImageAsset Include="Assets.xcassets\AppIcon.appiconset\Contents.json">

Просмотреть файл

@ -5,8 +5,7 @@
<AssemblyName>Microsoft.Azure.NotificationHubs.Client</AssemblyName>
<!-- Target iOS, tvOS, macOS, and Android -->
<TargetFrameworks>Xamarin.iOS10;Xamarin.TVOS10;Xamarin.Mac20;MonoAndroid90;monoandroid10.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>Xamarin.iOS10;Xamarin.TVOS10;Xamarin.Mac20;MonoAndroid10.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyFileVersion>1.0.0.0</AssemblyFileVersion>
@ -48,37 +47,19 @@
<!-- iOS files have ios.cs -->
<Compile Include="**\*.ios.cs" />
</ItemGroup>
</ItemGroup>
<!-- Add Xamarin Android bindings -->
<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<!-- Add Xamarin Android bindings -->
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.3" />
<PackageReference Include="Xamarin.Android.Volley" Version="1.1.1.1" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.2.1" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="120.1.7" />
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.v4" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Compat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.UI" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Fragment" Version="28.0.0.3" />
<PackageReference Include="Xamarin.Android.Support.Media.Compat" Version="28.0.0.3" />
<PackageReference Include="Xamarin.GooglePlayServices.Iid" Version="117.0.0" />
<!-- Android files have android.cs -->
<Compile Include="**\*.android.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('monoandroid10')) ">
<!-- Add Xamarin Android bindings -->
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.2.0.5" />
<PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.8" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.3" />
<PackageReference Include="Xamarin.Android.Volley" Version="1.1.1.1" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.2.1" />
<PackageReference Include="Xamarin.Google.Android.DataTransport.TransportBackendCct" Version="2.2.3" />
<PackageReference Include="Xamarin.Google.Android.DataTransport.TransportRuntime" Version="2.2.3" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="120.1.7" />
<PackageReference Include="Xamarin.GooglePlayServices.Iid" Version="117.0.0" />
<PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.2.1" />
<PackageReference Include="Xamarin.Android.Volley" Version="1.1.1.1" />
<PackageReference Include="Xamarin.Azure.NotificationHubs.Android" Version="1.1.3" />
<!-- Android files have android.cs -->
<Compile Include="**\*.android.cs" />
@ -123,5 +104,4 @@
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
</Project>
</Project>

Просмотреть файл

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
<PropertyGroup>
<TargetFramework>MonoAndroid90</TargetFramework>
<TargetFramework>MonoAndroid10.0</TargetFramework>
<IsBindingProject>true</IsBindingProject>
<AssemblyName>Xamarin.Azure.NotificationHubs.Android</AssemblyName>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
@ -11,6 +11,7 @@
<AndroidClassParser>class-parse</AndroidClassParser>
<AndroidCodegenTarget>XAJavaInterop1</AndroidCodegenTarget>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<PackageOutputPath>$(SolutionDir)\.artifacts</PackageOutputPath>
<RootNamespace>WindowsAzure.Messaging</RootNamespace>
<AssemblyName>Xamarin.Azure.NotificationHubs.Android</AssemblyName>
@ -27,7 +28,7 @@
<PackageProjectUrl>https://go.microsoft.com/fwlink/?linkid=864958</PackageProjectUrl>
<PackageLicenseUrl>https://go.microsoft.com/fwlink/?linkid=864960</PackageLicenseUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageVersion>1.1.2</PackageVersion>
<PackageVersion>1.1.3</PackageVersion>
</PropertyGroup>
<ItemGroup>
@ -41,10 +42,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="28.0.0.3" PrivateAssets="None" />
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.2.0.5" />
<PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.8" />
<PackageReference Include="Xamarin.Google.Android.DataTransport.TransportBackendCct" Version="2.2.2" />
<PackageReference Include="Xamarin.Google.Android.DataTransport.TransportRuntime" Version="2.2.2" />
<PackageReference Include="Xamarin.Firebase.Messaging" Version="120.1.7" />
<PackageReference Include="Xamarin.Android.Volley" Version="1.1.1.1" />
</ItemGroup>
</Project>

Просмотреть файл

@ -1,19 +1,19 @@
var TARGET = Argument ("t", Argument ("target", "ci"));
var IOS_VERSION = "3.1.2";
var IOS_NUGET_VERSION = "3.1.2";
var IOS_VERSION = "3.1.1";
var IOS_NUGET_VERSION = "3.1.1";
var IOS_URL = $"https://github.com/Azure/azure-notificationhubs-ios/releases/download/{IOS_VERSION}/WindowsAzureMessaging-SDK-Apple-{IOS_VERSION}.zip";
var ANDROID_VERSION = "1.1.2";
var ANDROID_NUGET_VERSION = "1.1.2";
var ANDROID_URL = string.Format ("https://dl.bintray.com/microsoftazuremobile/SDK/com/microsoft/azure/notification-hubs-android-sdk/{0}/notification-hubs-android-sdk-{0}.aar", ANDROID_VERSION);
var ANDROID_VERSION = "1.1.3";
var ANDROID_NUGET_VERSION = "1.1.3";
var ANDROID_URL = $"https://dl.bintray.com/microsoftazuremobile/SDK/com/microsoft/azure/notification-hubs-android-sdk/{0}/notification-hubs-android-sdk-{ANDROID_VERSION}.aar";
Task("libs-ios")
.WithCriteria(IsRunningOnUnix())
.IsDependentOn("externals-ios")
.Does (() =>
{
MSBuild("./iOS/Xamarin.Azure.NotificationHubs.iOS/Xamarin.Azure.NotificationHubs.iOS.sln", c => {
MSBuild("./iOS/source/Xamarin.Azure.NotificationHubs.iOS.sln", c => {
c.Configuration = "Release";
c.Restore = true;
c.Targets.Clear();
@ -30,7 +30,7 @@ Task("libs-android")
.IsDependentOn("externals-android")
.Does (() =>
{
MSBuild("./Android/Xamarin.Azure.NotificationHubs.Android/Xamarin.Azure.NotificationHubs.Android.sln", c => {
MSBuild("./Android/source/Xamarin.Azure.NotificationHubs.Android.sln", c => {
c.Configuration = "Release";
c.Restore = true;
c.Targets.Clear();
@ -48,7 +48,7 @@ Task("nuget-ios")
.IsDependentOn("libs-ios")
.Does (() =>
{
MSBuild ("./iOS/Xamarin.Azure.NotificationHubs.iOS/Xamarin.Azure.NotificationHubs.iOS.sln", c => {
MSBuild ("./iOS/source/Xamarin.Azure.NotificationHubs.iOS.sln", c => {
c.Configuration = "Release";
c.Targets.Clear();
c.Targets.Add("Pack");
@ -67,7 +67,7 @@ Task("nuget-android")
.IsDependentOn("libs-android")
.Does (() =>
{
MSBuild ("./Android/Xamarin.Azure.NotificationHubs.Android/Xamarin.Azure.NotificationHubs.Android.sln", c => {
MSBuild ("./Android/source/Xamarin.Azure.NotificationHubs.Android.sln", c => {
c.Configuration = "Release";
c.Targets.Clear();
c.Targets.Add("Pack");
@ -81,6 +81,41 @@ Task("nuget-android")
});
});
Task("samples-ios")
.WithCriteria(IsRunningOnUnix())
.IsDependentOn("nuget-ios")
.Does (() =>
{
MSBuild("./iOS/samples/NotificationHubsSampleiOS.sln", c => {
c.Configuration = "Release";
c.Restore = true;
c.Targets.Clear();
c.Targets.Add("Build");
c.BinaryLogger = new MSBuildBinaryLogSettings {
Enabled = true,
FileName = "./output/samples-ios.binlog"
};
});
});
Task("samples-android")
.IsDependentOn("nuget-android")
.Does (() =>
{
MSBuild("./Android/samples/NotificationHubsSampleAndroid.sln", c => {
c.Configuration = "Release";
c.Restore = true;
c.Targets.Clear();
c.Targets.Add("Build");
c.BinaryLogger = new MSBuildBinaryLogSettings {
Enabled = true,
FileName = "./output/samples-android.binlog"
};
});
});
Task("ci")
.IsDependentOn("nuget");
@ -112,7 +147,7 @@ Task ("externals-ios")
"./iOS/externals/WindowsAzureMessaging-SDK-Apple/tvOS/WindowsAzureMessaging.framework/WindowsAzureMessaging",
"./iOS/externals/tvOS/WindowsAzureMessaging.a");
XmlPoke("./iOS/Xamarin.Azure.NotificationHubs.iOS/Xamarin.Azure.NotificationHubs.iOS.csproj", "/Project/PropertyGroup/PackageVersion", IOS_NUGET_VERSION);
XmlPoke("./iOS/source/Xamarin.Azure.NotificationHubs.iOS.csproj", "/Project/PropertyGroup/PackageVersion", IOS_NUGET_VERSION);
});
Task ("externals-android")
@ -124,13 +159,17 @@ Task ("externals-android")
Information($"Downloading from {ANDROID_URL}");
DownloadFile (ANDROID_URL, "./Android/externals/notificationhubs.aar");
XmlPoke("./Android/Xamarin.Azure.NotificationHubs.Android/Xamarin.Azure.NotificationHubs.Android.csproj", "/Project/PropertyGroup/PackageVersion", ANDROID_NUGET_VERSION);
XmlPoke("./Android/source/Xamarin.Azure.NotificationHubs.Android.csproj", "/Project/PropertyGroup/PackageVersion", ANDROID_NUGET_VERSION);
});
Task ("externals")
.IsDependentOn ("externals-ios")
.IsDependentOn ("externals-android");
Task ("samples")
.IsDependentOn ("samples-ios")
.IsDependentOn ("samples-android");
Task ("nuget")
.IsDependentOn ("nuget-ios")
.IsDependentOn ("nuget-android");

Просмотреть файл

@ -20,7 +20,7 @@
<PackageProjectUrl>https://go.microsoft.com/fwlink/?linkid=864962</PackageProjectUrl>
<PackageLicenseUrl>https://go.microsoft.com/fwlink/?linkid=865062</PackageLicenseUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageVersion>3.1.2</PackageVersion>
<PackageVersion>3.1.1</PackageVersion>
</PropertyGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">