Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ public async Task SendEachForMulticast()
TimeToLive = TimeSpan.FromHours(1),
RestrictedPackageName = "com.google.firebase.testing",
},
#pragma warning disable CS0618
Tokens = new[]
{
"token1",
"token2",
},
#pragma warning restore CS0618
};
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(multicastMessage, dryRun: true);
Assert.NotNull(response);
Expand All @@ -133,6 +135,35 @@ public async Task SendEachForMulticast()
Assert.NotNull(response.Responses[1].Exception);
}

[Fact]
public async Task SendEachForMulticastFids()
{
var multicastMessage = new MulticastMessage
{
Notification = new Notification()
{
Title = "Title",
Body = "Body",
},
Android = new AndroidConfig()
{
Priority = Priority.Normal,
TimeToLive = TimeSpan.FromHours(1),
RestrictedPackageName = "com.google.firebase.testing",
},
Fids = new[]
{
"fid1",
"fid2",
},
};
var response = await FirebaseMessaging.DefaultInstance.SendEachForMulticastAsync(multicastMessage, dryRun: true);
Assert.NotNull(response);
Assert.Equal(2, response.FailureCount);
Assert.Equal(MessagingErrorCode.Unregistered, response.Responses[0].Exception.MessagingErrorCode);
Assert.Equal(MessagingErrorCode.Unregistered, response.Responses[1].Exception.MessagingErrorCode);
}

[Fact]
public async Task SubscribeToTopic()
{
Expand Down
81 changes: 81 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseExceptionSnippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,40 @@ internal static async Task PlatformErrorCode(string deviceToken)
// [END platform_error_code]
}

internal static async Task PlatformErrorCodeWithFid(string fid)
{
// [START platform_error_code_fid]
var notification = CreateNotificationWithFid(fid);
try
{
await FirebaseMessaging.DefaultInstance.SendAsync(notification);
}
catch (FirebaseMessagingException ex)
{
// All exceptions contain a platform-level error code. Applications can inspect
// both the platform-level error code and any service-level error codes when
// implementing error handling logic.
if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered)
{
// Service-level error code
Console.WriteLine("App instance has been unregistered");
RemoveFidFromDatabase(fid);
}
else if (ex.ErrorCode == ErrorCode.Unavailable)
{
// Platform-level error code
Console.WriteLine("FCM service is temporarily unavailable");
ScheduleForRetry(notification, TimeSpan.FromHours(1));
}
else
{
Console.WriteLine($"Failed to send notification: {ex.Message}");
}
}

// [END platform_error_code_fid]
}

internal static async Task HttpResponse(string deviceToken)
{
// [START http_response]
Expand Down Expand Up @@ -133,13 +167,58 @@ internal static async Task HttpResponse(string deviceToken)
// [END http_response]
}

internal static async Task HttpResponseWithFid(string fid)
{
// [START http_response_fid]
var notification = CreateNotificationWithFid(fid);
try
{
await FirebaseMessaging.DefaultInstance.SendAsync(notification);
}
catch (FirebaseMessagingException ex)
{
// If the exception was caused by a backend service error, applications can
// inspect the original error response received from the backend service to
// implement more advanced error handling behavior.
var response = ex.HttpResponse;
if (response != null)
{
Console.WriteLine($"FCM service responded with HTTP {response.StatusCode}");
foreach (var entry in response.Headers)
{
Console.WriteLine($">>> {entry.Key}: {entry.Value}");
}

var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(">>>");
Console.WriteLine($">>> {body}");
}
}

// [END http_response_fid]
}

private static void PerformPrivilegedOperation(string uid) { }

private static Message CreateNotification(string deviceToken)
{
return new Message()
{
#pragma warning disable CS0618
Token = deviceToken,
#pragma warning restore CS0618
Notification = new Notification()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add extend the code snippet to show the new usage with Fids?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Code snippets for the new usage with Fids are added.

{
Title = "Test notification",
},
};
}

private static Message CreateNotificationWithFid(string fid)
{
return new Message()
{
Fid = fid,
Notification = new Notification()
{
Title = "Test notification",
Expand All @@ -149,6 +228,8 @@ private static Message CreateNotification(string deviceToken)

private static void RemoveTokenFromDatabase(string deviceToken) { }

private static void RemoveFidFromDatabase(string fid) { }

private static void ScheduleForRetry(Message message, TimeSpan waitTime) { }
}
}
10 changes: 10 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseMessagingSnippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ internal static async Task SendToTokenAsync()
{ "score", "850" },
{ "time", "2:45" },
},
#pragma warning disable CS0618
Token = registrationToken,
#pragma warning restore CS0618
};

// Send a message to the device corresponding to the provided
Expand Down Expand Up @@ -105,7 +107,9 @@ internal static async Task SendDryRunAsync()
{ "score", "850" },
{ "time", "2:45" },
},
#pragma warning disable CS0618
Token = "token",
#pragma warning restore CS0618
};

// [START send_dry_run]
Expand All @@ -131,7 +135,9 @@ internal static async Task SendEachAsync()
Title = "Price drop",
Body = "5% off all electronics",
},
#pragma warning disable CS0618
Token = registrationToken,
#pragma warning restore CS0618
},
new Message()
{
Expand Down Expand Up @@ -164,7 +170,9 @@ internal static async Task SendEachForMulticastAsync()
};
var message = new MulticastMessage()
{
#pragma warning disable CS0618
Tokens = registrationTokens,
#pragma warning restore CS0618
Data = new Dictionary<string, string>()
{
{ "score", "850" },
Expand All @@ -191,7 +199,9 @@ internal static async Task SendEachForMulticastAndHandleErrorsAsync()
};
var message = new MulticastMessage()
{
#pragma warning disable CS0618
Tokens = registrationTokens,
#pragma warning restore CS0618
Data = new Dictionary<string, string>()
{
{ "score", "850" },
Expand Down
Loading
Loading