Tích hợp push notification .NET MAUI (C#)
C#, .NET 10. Android dùng Firebase, iOS dùng PushKit + CallKit trực tiếp.
🟣 .NET MAUI
C#, .NET 10. Android dùng Firebase, iOS dùng PushKit + CallKit trực tiếp.
Tải SDK trong bảng điều khiển (mục Hướng dẫn tích hợp) sau khi tạo tài khoản miễn phí.
Thêm SDK vào solution
Giải nén file tải về vào thư mục chứa solution (tạo ra thư mục
PushConsoleSDK/), tham chiếu project và khai báo file Firebase cho Android:MyApp.csproj<!-- MyApp.csproj --> <ItemGroup> <ProjectReference Include="PushConsoleSDK\maui\PushConsole.Maui\PushConsole.Maui.csproj" /> </ItemGroup> <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"> <GoogleServicesJson Include="Platforms\Android\google-services.json" /> </ItemGroup>Yêu cầu .NET 10 / MAUI 10. App .NET 9: đổi
TargetFrameworkstrongPushConsole.Maui.csproj. Trên TingPush: upload service account JSON (Android) và key .p8 (iOS).Khởi tạo trong MauiProgram
MauiProgram.csusing PushConsoleSdk; public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UsePushConsole(new PushConsoleOptions { AppId = "YOUR_APP_ID", ServerUrl = "https://tingpush.com", TextAnswer = "Nghe", TextDecline = "Từ chối" }); return builder.Build(); }iOS: đổi lớp cha AppDelegate, bật VoIP
Platforms/iOS/AppDelegate.cs// Platforms/iOS/AppDelegate.cs [Register("AppDelegate")] public class AppDelegate : PushConsoleSdk.PushConsoleAppDelegate // thay cho MauiUIApplicationDelegate { protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); }Platforms/iOS<!-- Platforms/iOS/Info.plist --> <key>UIBackgroundModes</key> <array> <string>remote-notification</string> <string>voip</string> </array> <!-- Platforms/iOS/Entitlements.plist (csproj: <CodesignEntitlements>Platforms/iOS/Entitlements.plist</CodesignEntitlements>) --> <key>aps-environment</key> <string>development</string>
Bật Push Notifications cho App ID trên Apple Developer. Bản phát hành:
aps-environment = production.Nhận sự kiện & gắn user
App.xaml.csusing PushConsoleSdk; public partial class App : Application { public App() { InitializeComponent(); PushConsole.CallEvent += (_, e) => { if (e.Action == PushConsoleCallAction.Accepted) { // Người dùng bấm Nghe → kết nối cuộc gọi (e.Call.Id, e.Call.Data["room"]) } }; PushConsole.NotificationOpened += (_, e) => { /* điều hướng theo e.Data */ }; } protected override void OnStart() { // Android: bấm Nghe lúc app đang tắt if (PushConsole.TakeAcceptedCall() is { } call) { /* kết nối cuộc gọi */ } } } // Gắn user await PushConsole.LoginAsync("user-123"); await PushConsole.LogoutAsync(); await PushConsole.SetTagsAsync(new Dictionary<string, string> { ["plan"] = "vip" });Android:
MainActivitygiữLaunchMode = LaunchMode.SingleTop(mặc định của template). Có sẵnCanUseFullScreenIntent(),OpenBatteryOptimizationSettings().
✅ Kiểm tra sau khi tích hợp
- Chạy app trên máy thật, bật log: thấy
[PushConsole] Đã đăng ký thiết bị …. - Tab Thiết bị của dự án: thiết bị xuất hiện; máy iOS có badge VoIP nếu dùng cuộc gọi.
- Tab Gửi thông báo: gửi thử tới thiết bị đó.
- Chọn 📞 Cuộc gọi, gửi thử khi app đang mở, khi đã tắt app và khi màn hình đang khoá.