Does MAUI support push notification

Anjana Jois 1 Reputation point
2022-08-04T08:30:59.637+00:00

Does MAUI preview 6 have option to support or handle push notifications using FCM.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,861 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Adel Alzubeir 6 Reputation points
    2022-08-31T09:15:36.65+00:00

    I originally wrote this in a comment, adding it here for visibility, hopefully it helps more people.

    I got it to work on Android, here is what i'm using and what i did

    1. Visual Studio 2022 17.3.2 (NOT PREVIEW), (NOTICE: 17.3.3 just got released few hours ago, and i'm not upgrading yet).
    2. In your project, install the following Nuget Packages:
    3. Xamarin.Firebase.Messaging v123.0.7 (Latest at the time of writing)
    4. Plugin.FirebasePushNotification v3.4.25 (Latest at the time of writing)
    5. RESTART VISUAL STUDIO
    6. Add your google-service.json to Project Folder/Platforms/Android
    7. Right click the file, click properties.
    8. In Build Actions, select GoogleServiceJson

    NOTICE: If you don't see Google ServicesJson, Restart Visual Studio.

    In App.xaml.cs add the following in the constructor.

    CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>  
             {  
                 // PLACE BREAK POINT HERE TO CAPTURE TOKEN IN DEBUG  
                 System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");  
             };  
             // Push message received event  
             CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>  
             {  
          
                 System.Diagnostics.Debug.WriteLine("Received");  
          
             };  
             //Push message received event  
             CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>  
             {  
                 System.Diagnostics.Debug.WriteLine("Opened");  
                 foreach (var data in p.Data)  
                 {  
                     System.Diagnostics.Debug.WriteLine($"{data.Key} : {data.Value}");  
                 }  
          
             };  
    

    In ProjectRoot/Platforms/Android/MainActivity.cs add the following

    protected override void OnCreate(Bundle savedInstanceState)  
             {  
                 base.OnCreate(savedInstanceState);  
                 FirebasePushNotificationManager.ProcessIntent(this, Intent);  
              
             }  
    

    In ProjectRoot/Platforms/Android/MainApplication.cs add the following

     public override void OnCreate()  
         {  
             base.OnCreate();  
          
             //Set the default notification channel for your app when running Android Oreo  
             if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)  
             {  
                 //Change for your default notification channel id here  
                 FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";  
          
                 //Change for your default notification channel name here  
                 FirebasePushNotificationManager.DefaultNotificationChannelName = "General";  
             }  
          
             //If debug you should reset the token each time.  
     #if DEBUG  
             FirebasePushNotificationManager.Initialize(this, true);  
     #else  
                   FirebasePushNotificationManager.Initialize(this,false);  
     #endif  
          
             //Handle notification when app is closed here  
             CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>  
             {  
             };  
         }  
      
    

    Place a break point inside the event OnTokenRefresh in App.xaml.cs, run. Get Token. Use that token to test using Firebase Console

    1 person found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,291 Reputation points Microsoft Vendor
    2022-08-05T08:07:16.477+00:00

    Hello,

    Pushing notification is supported in MAUI, it's similar to Xamarin.

    For using FCM in MAUI on Android, please refer to this GitHub issue Push Notification for MAUI #5458, and you could find the libraries needed in MAUI:

       <ItemGroup>  
           <PackageReference Include="Xamarin.Firebase.Messaging" Version="122.0.0" />'  
           <PackageReference Include="Xamarin.Google.Dagger" Version="2.39.1" />'  
           <PackageReference Include="Xamarin.GooglePlayServices.Base" Version="117.6.0" />'  
       </ItemGroup>  
    

    Please feel free to let me know if you have any other issues.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. David Friedel 6 Reputation points
    2022-09-11T08:41:13.33+00:00

    We had to tweak it slightly to get this to work in Maui. Specify these in the <ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">

    <PackageReference Include="Xamarin.Google.Guava" Version="31.1.0.1" />  
    <PackageReference Include="Xamarin.Google.Guava.ListenableFuture" Version="1.0.0.9" />  
    

    next, add these to the Project group above.

        <XamarinGoogleGuavaOptOut>true</XamarinGoogleGuavaOptOut>  
        <XamarinGoogleGuavaListenableFutureOptOut>true</XamarinGoogleGuavaListenableFutureOptOut>  
    

    If you have other small odd issues around Analytics by Google, we added these to align the packages

    <PackageReference Include="R8.GoogleAnalytics" Version="2.1.3" />  
    

    Lastly, to get the actual token to be requested, which was;t happening - we added this to the OnCreate() above the FirebasePushNotificationManager init.

    FirebaseApp.InitializeApp(this);  
    

    Hope this helps someone.

    0 comments No comments

  5. Muhab Abdulraheem 21 Reputation points
    2022-12-14T18:02:04.817+00:00

    Please refer to this working project: https://github.com/mahab339/Pushdemoformaui

    Pushdemoformaui

    Export xamarin with hubs sample to maui (https://github.com/xamcat/mobcat-samples/tree/master/notification_hub_backend_service/src/xamarin)

    Main changes:

    • Use net 7 for nuget packages compatibility.
    • For build action of google-servicesjson, install and import xamarin.googleplayservices.basement package to csproj file just before closing tag for project element. For example: <Import Project="C:\Users\Administrator\.nuget\packages\xamarin.googleplayservices.basement\118.1.0\build\MonoAndroid12.0\Xamarin.GooglePlayServices.Basement.targets" Condition="Exists('C:\Users\Administrator\.nuget\packages\xamarin.googleplayservices.basement\118.1.0\build\MonoAndroid12.0\Xamarin.GooglePlayServices.Basement.targets')" /> More on import element: https://learn.microsoft.com/en-us/visualstudio/msbuild/import-element-msbuild?view=vs-2022 Reference https://stackoverflow.com/questions/42677766/cant-set-build-action-to-googleservicejson
    • Move service regesteration from bootstrap to app.xaml.cs constructor if possible.
    • After each build, an error about auto generated AndroidManifest.xml. In (obj\Debug\net7.0-android\AndroidManifest.xml), add attribut (android:exported="true") to element service with (android:name) attribute value ending with PushNotificationFirebaseMessagingService and rebuild.