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
<PackageReference Include="benxu.AppPlatform.Firebase.Fcm" Version="3.0.2" />
<PackageVersion Include="benxu.AppPlatform.Firebase.Fcm" Version="3.0.2" />
<PackageReference Include="benxu.AppPlatform.Firebase.Fcm" />
paket add benxu.AppPlatform.Firebase.Fcm --version 3.0.2
#r "nuget: benxu.AppPlatform.Firebase.Fcm, 3.0.2"
#:package benxu.AppPlatform.Firebase.Fcm@3.0.2
#addin nuget:?package=benxu.AppPlatform.Firebase.Fcm&version=3.0.2
#tool nuget:?package=benxu.AppPlatform.Firebase.Fcm&version=3.0.2
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 (完整支援)
安裝
安裝套件:
dotnet add package benxu.AppPlatform.Firebase.Fcm平台設定:
- Android: 需設定
AndroidManifest.xml權限與google-services.json。 - iOS: 需設定
Info.plist與GoogleService-Info.plist,並啟用 Push Notifications Capability。
- Android: 需設定
使用方式
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();
}
// ... 事件處理
}
4. Deep Link 路由
@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.CloudMessagingbenxu.AppPlatform.Core
授權
MIT License - Copyright (c) 2025 benxu
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-android36.0 is compatible. net10.0-ios26.0 is compatible. |
-
net10.0-android36.0
- benxu.AppPlatform.Core (>= 3.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Maui.Controls (>= 10.0.0)
- Plugin.Firebase.CloudMessaging (>= 4.0.0)
- Xamarin.AndroidX.Activity (>= 1.10.1.3)
- Xamarin.AndroidX.Activity.Ktx (>= 1.10.1.3)
- Xamarin.AndroidX.Collection (>= 1.5.0.3)
- Xamarin.AndroidX.Collection.Jvm (>= 1.5.0.3)
- Xamarin.AndroidX.Collection.Ktx (>= 1.5.0.3)
- Xamarin.Build.Download (>= 0.11.4)
-
net10.0-ios26.0
- benxu.AppPlatform.Core (>= 3.0.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Microsoft.Maui.Controls (>= 10.0.0)
- Plugin.Firebase.CloudMessaging (>= 4.0.0)
- Xamarin.Build.Download (>= 0.11.4)
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.