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.
SDK miễn phí, mã nguồn mở (MIT) trên GitHub. Tạo tài khoản miễn phí để có App ID.
Thêm SDK
Chạy
dotnet add package TingPush.Maui(gói TingPush.Maui trên NuGet), rồi khai báo file Firebase cho Android:MyApp.csproj<!-- MyApp.csproj --> <ItemGroup> <PackageReference Include="TingPush.Maui" Version="1.1.1" /> </ItemGroup> <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'"> <GoogleServicesJson Include="Platforms\Android\google-services.json" /> </ItemGroup>Yêu cầu .NET 10 / MAUI 10. Trên TingPush: upload service account JSON (Android) và key .p8 (iOS).
Khởi tạo trong MauiProgram
MauiProgram.csusing TingPushSdk; public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UseTingPush(new TingPushOptions { AppId = "YOUR_APP_ID", 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 : TingPushSdk.TingPushAppDelegate // 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 TingPushSdk; public partial class App : Application { public App() { InitializeComponent(); TingPush.CallEvent += (_, e) => { if (e.Action == TingPushCallAction.Accepted) { // Người dùng bấm Nghe → kết nối cuộc gọi (e.Call.Id, e.Call.Data["room"]) } }; // Người dùng bấm vào thông báo (kể cả lúc app đang tắt) TingPush.NotificationOpened += (_, e) => { /* điều hướng theo e.Data["screen"] */ }; // Nhận thông báo khi app đang mở TingPush.NotificationReceived += (_, e) => { /* e.Title, e.Body, e.Data */ }; } protected override void OnStart() { // Android: bấm Nghe lúc app đang tắt if (TingPush.TakeAcceptedCall() is { } call) { /* kết nối cuộc gọi */ } } } // Gắn user await TingPush.LoginAsync("user-123"); await TingPush.LogoutAsync(); await TingPush.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
[TingPush] Đã đă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á.