Skip to content

Commit

Permalink
[firebase] Update sample and use one binding project (#16)
Browse files Browse the repository at this point in the history
The analytics and messaging native projects have been merged into a
single MauiFirebase Xcode project.  The sample has been updated to use
bound APIs from both the analytics and messaging SDKs.
  • Loading branch information
pjcollins authored May 1, 2024
1 parent d40ccc7 commit 8a0bfe3
Show file tree
Hide file tree
Showing 34 changed files with 491 additions and 966 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Get started with slim bindings using the Facebook, Firebase Analytics, Firebase
1. Submodule or clone this repo

2. Navigate to the appropriate folder for the binding you're interested in using or building from <br>
e.g. For Firebase Messaging, navigate to `firebase/macios/FirebaseMessaging.Binding`
e.g. For Firebase Messaging, navigate to `firebase/macios/Firebase.MaciOS.Binding`

3. Run `dotnet build`

Expand All @@ -24,7 +24,7 @@ e.g. For Firebase Messaging, navigate to `firebase/macios/FirebaseMessaging.Bind
5. Add a project reference to your MAUI app pointing to the path where you have cloned the repo <br>
e.g. For Firebase Messaging, add to your csproj:
```xaml
<ProjectReference Include="<YourPathToClonedSlimBindingsRepo>\FirebaseMessaging.Binding\FirebaseMessaging.Binding.csproj" />
<ProjectReference Include="<YourPathToClonedSlimBindingsRepo>\Firebase.MaciOS.Binding\Firebase.MaciOS.Binding.csproj" />
```

6. Cross-reference the sample and add the necessary code into your own project. <br>
Expand Down Expand Up @@ -131,8 +131,8 @@ If the existing API surface in a given sample doesn't expose the functionality y
Inside the Xcode project you will find one or more .Swift files which define the public API surface for the Slim Binding. For example, the `register` method for Firebase Messaging is defined as below:

```Swift
@objc(FirebaseMessaging)
public class FirebaseMessaging : NSObject {
@objc(MauiFIRMessaging)
public class MauiFIRMessaging : NSObject {

@objc(register:completion:)
public static func register(apnsToken: NSData, completion: @escaping (String?, NSError?) -> Void) {
Expand All @@ -153,25 +153,24 @@ public class FirebaseMessaging : NSObject {

You can see in this method that the public API surface only uses types which iOS for .NET already is aware of: `NSData`, `String`, `NSError` and a callback.

In the `FirebaseMessaging.Binding` project, the `ApiDefinitions.cs` file contains the binding definition for this slim wrapper API:
In the `Firebase.MaciOS.Binding` project, the `ApiDefinitions.cs` file contains the binding definition for this slim wrapper API:

```csharp
using System;
using Foundation;

namespace Firebase
{
[BaseType(typeof(NSObject))]
interface FirebaseMessaging
// @interface MauiFIRMessaging : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRMessaging
{
[Static]
[Export("register:completion:")]
[Export ("register:completion:")]
[Async]
void Register(NSData nativePush, Action<string?, NSError?> completion);

void Register (NSData apnsToken, Action<string?, NSError?> completion);
// ...
}
}
```

#### Modifying Mac/iOS
Expand All @@ -191,7 +190,7 @@ public static func unregister(completion: @escaping (NSError?) -> Void) {
The other half will be to update the `ApiDefinitions.cs` file in the binding project to expose this new method. There are two ways you can go about this:

1. You can manually add the required code
2. When the binding project builds, objective sharpie is run and an `ApiDefinitions.cs` file is generated inside of the `native/macios/messaging/.build/Binding` folder (this path will vary based on the project you are working on of course). You can try to find the relevant changes from this file and copy them over manually, or try copying over the whole file and looking at the diff to find the part you need.
2. When the binding project builds, objective sharpie is run and an `ApiDefinitions.cs` file is generated inside of the `native/macios/.build/Binding` folder (this path will vary based on the project you are working on of course). You can try to find the relevant changes from this file and copy them over manually, or try copying over the whole file and looking at the diff to find the part you need.

In this case, the changes to `ApiDefinitions.cs` would be:

Expand Down
97 changes: 97 additions & 0 deletions firebase/macios/Firebase.MaciOS.Binding/ApiDefinitions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#nullable enable
using System;
using Foundation;

namespace Firebase
{
// @interface MauiFIRAnalytics : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRAnalytics
{
// +(void)logEventWithEventName:(NSString * _Nonnull)eventName parameters:(NSDictionary<NSString *,id> * _Nonnull)parameters;
[Static]
[Export ("logEventWithEventName:parameters:")]
void LogEvent (string eventName, NSDictionary parameters);

// +(void)getAppInstanceIdWithCompletion:(void (^ _Nonnull)(NSString * _Nullable))completion;
[Static]
[Export ("getAppInstanceIdWithCompletion:")]
[Async]
void GetAppInstanceId (Action<NSString?> completion);

// +(void)setUserIdWithUserId:(NSString * _Nonnull)userId;
[Static]
[Export ("setUserIdWithUserId:")]
void SetUserId (string userId);

// +(void)setUserPropertyWithPropertyName:(NSString * _Nonnull)propertyName value:(NSString * _Nonnull)value;
[Static]
[Export ("setUserPropertyWithPropertyName:value:")]
void SetUserProperty (string propertyName, string value);

// +(void)setSessionTimeoutWithSeconds:(NSInteger)seconds;
[Static]
[Export ("setSessionTimeoutWithSeconds:")]
void SetSessionTimeout (nint seconds);

// +(void)resetAnalyticsData;
[Static]
[Export ("resetAnalyticsData")]
void ResetAnalyticsData ();
}

// @interface MauiFIRApp : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRApp
{
// +(void)autoConfigure;
[Static]
[Export ("autoConfigure")]
void AutoConfigure ();

// +(void)configure:(NSString * _Nonnull)googleAppId gcmSenderId:(NSString * _Nonnull)gcmSenderId;
[Static]
[Export ("configure:gcmSenderId:")]
void Configure (string googleAppId, string gcmSenderId);
}

// @interface MauiFIRMessaging : NSObject
[BaseType (typeof(NSObject))]
interface MauiFIRMessaging
{
// +(BOOL)getIsAutoInitEnabled __attribute__((warn_unused_result("")));
// +(void)setIsAutoInitEnabled:(BOOL)enabled;
[Static]
[Export ("getIsAutoInitEnabled")]
bool IsAutoInitEnabled { get; [Bind("setIsAutoInitEnabled:")] set; }

// +(NSString * _Nullable)getFcmToken __attribute__((warn_unused_result("")));
[Static]
[NullAllowed, Export ("getFcmToken")]
string FcmToken { get; }

// +(void)register:(NSData * _Nonnull)apnsToken completion:(void (^ _Nonnull)(NSString * _Nullable, NSError * _Nullable))completion;
[Static]
[Export ("register:completion:")]
[Async]
void Register (NSData apnsToken, Action<string?, NSError?> completion);

// +(void)unregister:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("unregister:")]
[Async]
void Unregister (Action<NSError?> completion);

// +(void)subscribe:(NSString * _Nonnull)topic completion:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("subscribe:completion:")]
[Async]
void Subscribe (string topic, Action<NSError?> completion);

// +(void)unsubscribe:(NSString * _Nonnull)topic completion:(void (^ _Nonnull)(NSError * _Nullable))completion;
[Static]
[Export ("unsubscribe:completion:")]
[Async]
void Unsubscribe (string topic, Action<NSError?> completion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<IsBindingProject>true</IsBindingProject>
<NoBindingEmbedding>true</NoBindingEmbedding>

<XcodeProject>$(MSBuildThisFileDirectory)../native/analytics/MauiFirebaseAnalytics.xcodeproj</XcodeProject>
<XcodeProject>$(MSBuildThisFileDirectory)../native/MauiFirebase.xcodeproj</XcodeProject>
<XcodeBuildXCFramework>true</XcodeBuildXCFramework>
<ObjSharpieBind>False</ObjSharpieBind>
<ObjSharpieBind>true</ObjSharpieBind>
<ObjSharpieBindNamespace>Firebase</ObjSharpieBindNamespace>
</PropertyGroup>

<ItemGroup>
<ObjcBindingApiDefinition Include="ApiDefinitions.cs" />
<NativeReference Include="../native/analytics/.build/MauiFirebaseAnalytics.xcframework">
<NativeReference Include="../native/.build/MauiFirebase.xcframework">
<Kind>Framework</Kind>
<SmartLink>True</SmartLink>
<SmartLink>true</SmartLink>
</NativeReference>
</ItemGroup>

Expand Down
8 changes: 1 addition & 7 deletions firebase/macios/Firebase.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseAnalytics.Binding", "FirebaseAnalytics.Binding\FirebaseAnalytics.Binding.csproj", "{3ED9CE50-B781-4DDB-831E-36544D8F9C87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirebaseMessaging.Binding", "FirebaseMessaging.Binding\FirebaseMessaging.Binding.csproj", "{53C8A442-72AB-4AD2-A188-B30E07160FEB}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Firebase.Macios.Binding", "Firebase.Macios.Binding\Firebase.Macios.Binding.csproj", "{3ED9CE50-B781-4DDB-831E-36544D8F9C87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample", "sample\Sample.csproj", "{1FEADCF2-520E-4B2B-AFA6-4E5291034DEC}"
EndProject
Expand All @@ -23,10 +21,6 @@ Global
{3ED9CE50-B781-4DDB-831E-36544D8F9C87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3ED9CE50-B781-4DDB-831E-36544D8F9C87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3ED9CE50-B781-4DDB-831E-36544D8F9C87}.Release|Any CPU.Build.0 = Release|Any CPU
{53C8A442-72AB-4AD2-A188-B30E07160FEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{53C8A442-72AB-4AD2-A188-B30E07160FEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{53C8A442-72AB-4AD2-A188-B30E07160FEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{53C8A442-72AB-4AD2-A188-B30E07160FEB}.Release|Any CPU.Build.0 = Release|Any CPU
{1FEADCF2-520E-4B2B-AFA6-4E5291034DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FEADCF2-520E-4B2B-AFA6-4E5291034DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FEADCF2-520E-4B2B-AFA6-4E5291034DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
54 changes: 0 additions & 54 deletions firebase/macios/FirebaseAnalytics.Binding/ApiDefinitions.cs

This file was deleted.

38 changes: 0 additions & 38 deletions firebase/macios/FirebaseMessaging.Binding/ApiDefinitions.cs

This file was deleted.

This file was deleted.

23 changes: 18 additions & 5 deletions firebase/macios/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# MAUI
A set of examples for slim binding native libraries for use with .NET MAUI apps
# Firebase Slim Binding
This folder contains a slim binding for the Firebase SDK which demonstrates simple [Analytics][0] and [Messaging][1] usage.

### Build and Run
```shell
dotnet build sample -t:Run -f net8.0-ios
```

## Testing
Add your GoogleService-Info.plist to the sample project and change <ApplicationId> to your
iOS app identifier in the Sample.csproj
### Configure
The included sample requires some modification to fully function. You will need to log in to
a Firebase developer account and configure an app to interface with this sample.
For more details, reference the [Get Started (iOS)][2] page.

1. Download your `GoogleService-Info.plist` and replace `Platforms/iOS/GoogleService-Info.plist` with it.
2. Change the `<ApplicationId>` value in `Sample.csproj` to your Firebase iOS app identifier.


[0]: https://firebase.google.com/docs/analytics/get-started?platform=ios
[1]: https://firebase.google.com/docs/cloud-messaging/ios/client
[2]: https://firebase.google.com/docs/ios/setup
3 changes: 3 additions & 0 deletions firebase/macios/native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build/
build/
deps/
Loading

0 comments on commit 8a0bfe3

Please sign in to comment.