File size: 2,630 Bytes
ccc2569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@page "/fcm-test"
@inject IJSRuntime JSRuntime
@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]

<div class="p-6 max-w-4xl mx-auto">
    <div class="card p-6 bg-white shadow-md rounded-lg">
        <h1 class="text-2xl font-bold text-mystic-900 mb-4">FCM Token Test</h1>
        
        <p class="text-mystic-600 mb-6">
            Click the button below to request notification permission and generate your FCM token.
        </p>

        <div class="flex flex-col gap-4">
            <button @onclick="GetToken" class="btn-primary w-fit">
                Get FCM Token
            </button>

            @if (!string.IsNullOrEmpty(Token))
            {
                <div class="mt-4 p-4 bg-mystic-100 rounded-lg border border-mystic-200">
                    <p class="text-sm font-semibold text-mystic-900 mb-2">Your Token:</p>
                    <div class="flex gap-2">
                        <textarea readonly class="w-full p-2 text-xs font-mono bg-white border border-mystic-300 rounded" rows="4">@Token</textarea>
                        <button @onclick="CopyToken" class="btn-secondary h-fit">Copy</button>
                    </div>
                    <p class="mt-2 text-xs text-mystic-500">Copy this token and use it to test your Backend API.</p>
                </div>
            }

            @if (IsLoading)
            {
                <p class="text-mystic-500 italic">Requesting token...</p>
            }

            @if (!string.IsNullOrEmpty(ErrorMessage))
            {
                <p class="text-red-500 text-sm">@ErrorMessage</p>
            }
        </div>
    </div>
</div>

@code {
    private string? Token { get; set; }
    private string? ErrorMessage { get; set; }
    private bool IsLoading { get; set; }

    private async Task GetToken()
    {
        IsLoading = true;
        ErrorMessage = null;
        try
        {
            Token = await JSRuntime.InvokeAsync<string?>("getFcmToken");
            if (string.IsNullOrEmpty(Token))
            {
                ErrorMessage = "Failed to get token. Check if you granted permission or if VAPID key is configured.";
            }
        }
        catch (Exception ex)
        {
            ErrorMessage = $"Error: {ex.Message}";
        }
        finally
        {
            IsLoading = false;
        }
    }

    private async Task CopyToken()
    {
        if (!string.IsNullOrEmpty(Token))
        {
            await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", Token);
            await JSRuntime.InvokeVoidAsync("showToast", "Token copied to clipboard!", "success");
        }
    }
}