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.

  1. 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).

  2. Khởi tạo trong MauiProgram

    MauiProgram.cs
    using 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();
    }
  3. 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.

  4. Nhận sự kiện & gắn user

    App.xaml.cs
    using 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: MainActivity giữ LaunchMode = LaunchMode.SingleTop (mặc định của template). Có sẵn CanUseFullScreenIntent(), OpenBatteryOptimizationSettings().

✅ Kiểm tra sau khi tích hợp

  1. Chạy app trên máy thật, bật log: thấy [TingPush] Đã đăng ký thiết bị ….
  2. 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.
  3. Tab Gửi thông báo: gửi thử tới thiết bị đó.
  4. 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á.

Sẵn sàng tích hợp?

Tạo tài khoản để nhận App ID và API Key. Code mẫu trong bảng điều khiển được điền sẵn thông tin dự án của bạn.