Coder Social home page Coder Social logo

betterfirebasenotificationsplugin's Introduction

BetterFireBaseNotificationsPlugin

Attempting to upgrade CrossFireBasePlugin to MAUI

Been using https://github.com/CrossGeeks/FirebasePushNotificationPlugin for my Xamarin app. this doesnt work for MAUI .net7 so im trying to upgrade it to maui .net7

So far so good

For now this project builds and can be added to a solution. If you want to use it as is just clone it and add it to your solution. then in your app go to dependencies and add it as a project dependency.

You may need to add to your project.csproj file

What works so far in Android

  • Basic Notifications
  • Notifications with Data
  • Notifications with user actions like in in the crossgeeks library
  • Silent notifications in foreground and background
  • Notifications when the app is closed/killed. still have to test this / start on it.

What works so far in iOS

  • Basic Notifications
  • Notifications with Data
  • Notifications with user actions like in in the crossgeeks library
  • Silent notifications in foreground and background

Note on useractions on ios

Apple displays these weird, to me at least. you have to swipe down the notification to see the actions. just clicking on it doesnt work and gets treated as the notification being opened. You can, like I did, check in your OnNotificationOpened if the notification has a click_action and use a DisplayAlert to give the user a chance to respond. Same goes on android. if you click on the notification instead of on one of the actions it gets treated as OnNotificationOpened.

My app project

My app is a default maui app without blazor

 <TargetFrameworks>net7.0-android33.0;net7.0-ios</TargetFrameworks>  
    <UseMaui>true</UseMaui>
    <SingleProject>true</SingleProject>
    <ImplicitUsings>enable</ImplicitUsings>
<OutputType>Exe</OutputType>

These are my other references

 <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android33.0'">
    <PackageReference Include="Xamarin.AndroidX.Activity" Version="1.7.2.1" />
    <PackageReference Include="Xamarin.AndroidX.Activity.Ktx" Version="1.7.2.1" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Camera.MAUI" Version="1.4.4" />
    <PackageReference Include="CClarke.Plugin.Calendars" Version="1.1.0" />
    <PackageReference Include="CommunityToolkit.Maui" Version="5.2.0" />
    <PackageReference Include="Geolocator.Plugin" Version="1.0.2" />
    <PackageReference Include="Microsoft.AppCenter" Version="5.0.2">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.2" />
    <PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.2" />
    <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
    <PackageReference Include="NdefLibrary" Version="4.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
    <PackageReference Include="Plugin.LocalNotification" Version="10.0.3" />
    <PackageReference Include="Plugin.Maui.Audio" Version="1.0.0" />
    <PackageReference Include="Plugin.NFC" Version="0.1.26" />
    <PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
    <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
    <PackageReference Include="ZXing.Net" Version="0.16.9" />
    <PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
    <PackageReference Include="SQLiteNetExtensions.Async" Version="2.1.0" />
    <PackageReference Include="SQLitePCLRaw.core" Version="2.1.0-pre20220207221914">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
    <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.0-pre20220207221914">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
    <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.0-pre20220207221914" />
    <PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.0-pre20220207221914">
      <TreatAsUsed>true</TreatAsUsed>
    </PackageReference>
  </ItemGroup>

MAUI Setup

Some of the setup takes place in your MauiProgram and some takes place in your app.cs. First off your google-services.json You stick this in your root folder and set its build action to GoogleServicesJson

Then your MauiProgram This is the same as how it is in the platforms of crossgeeks on your builder add the following

private static MauiAppBuilder RegisterFirebaseServices(this MauiAppBuilder builder)
    {
        builder.ConfigureLifecycleEvents(events => {
#if IOS
            events.AddiOS(iOS => iOS.WillFinishLaunching((_, options) =>
            {
                var usercategories = new NotificationUserCategory[]
            {
                new NotificationUserCategory("StillAlive", new List<NotificationUserAction>
                {
                    new NotificationUserAction("Yes","Yes", NotificationActionType.Foreground),
                    new NotificationUserAction("No","No", NotificationActionType.Foreground)

                })
               

            };
                //If you dont want useractions call one of the other initialize options
                FirebasePushNotificationManager.Initialize(options, usercategories, true);
                return false;
            }));
           
#elif ANDROID
            var usercategories = new NotificationUserCategory[]
            {
                new NotificationUserCategory("StillAlive", new List<NotificationUserAction>
                {
                    new NotificationUserAction("Yes","Yes", NotificationActionType.Foreground),
                    new NotificationUserAction("No","No", NotificationActionType.Foreground)

                })


            };

            events.AddAndroid(android => android.OnCreate((activity, _) =>
            //If you dont want useractions call one of the other initialize options
            FirebasePushNotificationManager.Initialize(usercategories, true, false, true)

            ));
#endif
        });
  builder.Services.AddSingleton<IPushNotificationHandler, DefaultPushNotificationHandler>();
  return builder;
}

This initializes firebase notifications. Now in your app.cs or wherever you want you can add your handler functions. like

BetterFirebasePushNotification.Current.OnNotificationReceived += async (s, p) =>
{
  //p = your notification
}

Or maybe you want to listen for token refreshes and send the new token to a backend

 BetterFirebasePushNotification.Current.OnTokenRefresh += async (s, p) =>
{
      if (p.Token != null && p.Token != "")
      {
                    var request = new RegistrationUpdate()
                    {
                        RegToken = p.Token,
                        SerialKey = Helpers.Settings.License.Replace("/", "").Replace("\\", "").Replace('"', ' ').Trim()
                    };
                    var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
                    await RestManager.Instance.ExecuteRest("SetPushtoken", stringContent);
      }
}

What Now

Since most scenarios work now im going to convert this to a Nuget package.

betterfirebasenotificationsplugin's People

Contributors

gekidoku avatar

Stargazers

Vitaly Filatenko avatar Muhieddine Kassem avatar Media Explorer avatar Giuseppe Novielli avatar khalil2099 avatar Juan Tamburini avatar Adrián Vergara Rivero avatar  avatar Abdelrahman Hamdy avatar Gleb Bakanov avatar SEOKWON HONG avatar kafo avatar Michael Ploy avatar Thomas Galliker avatar Juan Pablo Ibañez avatar  avatar Juan Rodríguez avatar  avatar  avatar

Watchers

 avatar  avatar Marc-Anthony Latty avatar Adrián Vergara Rivero avatar

betterfirebasenotificationsplugin's Issues

Token on IOS but no notifications

I am able to get a token on IOS, but when i try to send a notification to the device/topic nothing ever comes. Works fine in android. I would assume if it gets the token, everything is working like it should, but no event ever gets fired, firebase sends a good response, so i know the token is good.. any ideas?

Could not find a part of the path 'C:\Documents'

I pulled the latest code and changed the csproj file content back to dotnet 7 as it was on dotnet 8.

I attempt to buld the package with no other changes and receive

Severity	Code	Description	Project	File	Line	Suppression State
Error		Could not find a part of the path 'C:\Documents'.	Plugin.BetterFirebasePushNotification	C:\Program Files\dotnet\sdk\7.0.403\Sdks\NuGet.Build.Tasks.Pack\buildCrossTargeting\NuGet.Build.Tasks.Pack.targets	221	

Am I missing something?

All my other MAUI apps works fine

Sample App ?

Hi
Like you I would like to use the plugin in .net 7 and soon .net 8. I see that you have made a plugin that supports Maui.
Do you have a sample androidManifest.xml and what you put there.

Or even better a sample maui app where I can just replace the google-services.json.

Anyways thanks for your effort

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.