benxu.AppPlatform.Firebase.Fcm 3.0.2

dotnet add package benxu.AppPlatform.Firebase.Fcm --version 3.0.2
                    
NuGet\Install-Package benxu.AppPlatform.Firebase.Fcm -Version 3.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="benxu.AppPlatform.Firebase.Fcm" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="benxu.AppPlatform.Firebase.Fcm" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="benxu.AppPlatform.Firebase.Fcm" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add benxu.AppPlatform.Firebase.Fcm --version 3.0.2
                    
#r "nuget: benxu.AppPlatform.Firebase.Fcm, 3.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package benxu.AppPlatform.Firebase.Fcm@3.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=benxu.AppPlatform.Firebase.Fcm&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=benxu.AppPlatform.Firebase.Fcm&version=3.0.2
                    
Install as a Cake Tool

benxu.AppPlatform.Firebase.Fcm

Firebase Cloud Messaging (FCM) 實作套件 - 使用官方/社群套件 (Plugin.Firebase.CloudMessaging) 提供推播通知服務與 Deep Link 路由功能。

特色

  • 真實 FCM 整合:使用 Plugin.Firebase.CloudMessaging 進行原生 API 呼叫。
  • 推播通知接收:支援前景與背景通知接收。
  • Deep Link 路由:內建 Deep Link 解析與導航機制。
  • Device Token 管理:自動取得與更新 FCM Token。
  • 權限管理:封裝各平台的通知權限請求邏輯。

支援平台

  • Android (完整支援)
  • iOS (完整支援)

安裝

  1. 安裝套件:

    dotnet add package benxu.AppPlatform.Firebase.Fcm
    
  2. 平台設定:

    • Android: 需設定 AndroidManifest.xml 權限與 google-services.json
    • iOS: 需設定 Info.plistGoogleService-Info.plist,並啟用 Push Notifications Capability。

使用方式

1. 平台初始化

iOS 平台

在 iOS 專案的 AppDelegate.cs 中初始化 FCM:

using benxu.AppPlatform.Firebase.Fcm.Platforms.iOS;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        // 初始化 FCM(啟用前景通知)
        _ = FcmAppDelegate.InitializeFcmAsync(showForegroundNotifications: true);

        return base.FinishedLaunching(application, launchOptions);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        FcmAppDelegate.HandleRegisteredForRemoteNotifications(deviceToken);
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        FcmAppDelegate.HandleFailedToRegisterForRemoteNotifications(error);
    }

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
        Action<UIBackgroundFetchResult> completionHandler)
    {
        FcmAppDelegate.HandleDidReceiveRemoteNotification(userInfo, completionHandler);
    }

    public override void OnActivated(UIApplication application)
    {
        FcmAppDelegate.HandleOnActivated();
        base.OnActivated(application);
    }
}
Android 平台

Android 平台已自動處理,無需額外初始化代碼。

2. 註冊服務

MauiProgram.cs 中:

using benxu.AppPlatform.MAUI.Bootstrap.Extensions;

builder.UseAppPlatform(options =>
{
    options.UseFcm(fcm =>
    {
        fcm.EnableForegroundNotifications = true;
        fcm.DefaultNotificationChannelId = "my_channel";
        fcm.AutoSubscribeTopics = new List<string> { "global" };
    });
});

3. 應用程式初始化

App.razor 或主要 Layout 初始化並訂閱事件:

@inject IPushNotificationService PushService

@code {
    protected override async Task OnInitializedAsync()
    {
        PushService.NotificationReceived += OnNotificationReceived;
        PushService.NotificationTapped += OnNotificationTapped;

        await PushService.InitializeAsync();
    }

    // ... 事件處理
}
@inject IDeepLinkRouter DeepLinkRouter

// 註冊路由
DeepLinkRouter.RegisterRouteHandler("myapp", async (deepLink) =>
{
    if (deepLink.Host == "product")
    {
        var id = deepLink.QueryParameters.GetValueOrDefault("id");
        Navigation.NavigateTo($"/products/{id}");
        return Result.Success();
    }
    return Result.Failure("Unknown route");
});

iOS 平台專用功能

通知 Helper 工具

FcmNotificationHelper 提供 iOS 專用的通知管理功能:

using benxu.AppPlatform.Firebase.Fcm.Platforms.iOS;

// 請求通知權限
var granted = await FcmNotificationHelper.RequestNotificationPermissionAsync();

// 檢查通知權限
var hasPermission = await FcmNotificationHelper.HasNotificationPermissionAsync();

// 清除所有通知
FcmNotificationHelper.ClearAllNotifications();

// 設定 Badge 數字
FcmNotificationHelper.SetBadgeCount(5);

// 顯示本地通知
await FcmNotificationHelper.ShowLocalNotificationAsync("標題", "內容", data);

通知委派事件

FcmAppDelegate 提供事件訂閱功能:

// 訂閱前景通知接收事件
FcmAppDelegate.SubscribeNotificationReceived((sender, e) =>
{
    Console.WriteLine($"收到通知: {e.Title} - {e.Body}");
});

// 訂閱通知點擊事件
FcmAppDelegate.SubscribeNotificationTapped((sender, e) =>
{
    Console.WriteLine($"點擊通知: {e.Title}");
    // 處理 Deep Link 或導航
});

注意事項

  • 模擬模式:目前尚未實作,請使用真實裝置測試推播功能。
  • iOS 權限:iOS 模擬器無法接收遠端推播,必須使用實機測試。
  • iOS 前景通知:需在 AppDelegate.cs 中呼叫 FcmAppDelegate.InitializeFcmAsync() 並設定 showForegroundNotifications: true
  • iOS Push Notifications Capability:必須在 Xcode 中啟用 Push Notifications Capability。

依賴項目

  • Plugin.Firebase.CloudMessaging
  • benxu.AppPlatform.Core

授權

MIT License - Copyright (c) 2025 benxu

Product Compatible and additional computed target framework versions.
.NET net10.0-android36.0 is compatible.  net10.0-ios26.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on benxu.AppPlatform.Firebase.Fcm:

Package Downloads
benxu.AppPlatform.MAUI.Bootstrap

Bootstrap package for benxu App Platform. Provides fluent API for one-line service registration and lifecycle management.

benxu.AppPlatform.Notification

跨平台通知服務套件,提供 Android 和 iOS 的推播通知、本地通知管理功能,整合 Firebase Cloud Messaging (FCM) 服務,支援通知排程、通知歷程記錄與本地儲存,適用於 .NET MAUI Blazor 應用程式開發。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.2 87 1/17/2026
3.0.0 87 1/13/2026
2.0.0 84 1/11/2026
1.0.0 101 12/28/2025