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í.

  1. 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 TargetFrameworks trong PushConsole.Maui.csproj. Trên TingPush: upload service account JSON (Android) và key .p8 (iOS).

  2. Khởi tạo trong MauiProgram

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

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

    App.xaml.cs
    using 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: 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 [PushConsole] Đã đă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, API Key và tải SDK. Code mẫu trong bảng điều khiển được điền sẵn thông tin dự án của bạn.