|
| 1 | +// Copyright 2020, Google Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using FirebaseAdmin.Auth; |
| 18 | +using FirebaseAdmin.Messaging; |
| 19 | + |
| 20 | +namespace FirebaseAdmin.Snippets |
| 21 | +{ |
| 22 | + internal class FirebaseExceptionSnippets |
| 23 | + { |
| 24 | + internal static async Task GenericErrorHandler(string idToken) |
| 25 | + { |
| 26 | + // [START generic_error_handler] |
| 27 | + try |
| 28 | + { |
| 29 | + var token = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken); |
| 30 | + PerformPrivilegedOperation(token.Uid); |
| 31 | + } |
| 32 | + catch (FirebaseAuthException ex) |
| 33 | + { |
| 34 | + // A generic error handler that logs the error message, and ignores all |
| 35 | + // other properties. |
| 36 | + Console.WriteLine($"Failed to verify ID token: {ex.Message}"); |
| 37 | + } |
| 38 | + |
| 39 | + // [END generic_error_handler] |
| 40 | + } |
| 41 | + |
| 42 | + internal static async Task ServiceErrorCode(string idToken) |
| 43 | + { |
| 44 | + // [START service_error_code] |
| 45 | + try |
| 46 | + { |
| 47 | + var token = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken); |
| 48 | + PerformPrivilegedOperation(token.Uid); |
| 49 | + } |
| 50 | + catch (FirebaseAuthException ex) |
| 51 | + { |
| 52 | + // Some exception types expose a service-specific error code. Applications can |
| 53 | + // implement custom error handling logic based on these error codes. |
| 54 | + if (ex.AuthErrorCode == AuthErrorCode.ExpiredIdToken) |
| 55 | + { |
| 56 | + Console.WriteLine("ID token has expired"); |
| 57 | + } |
| 58 | + else if (ex.AuthErrorCode == AuthErrorCode.InvalidIdToken) |
| 59 | + { |
| 60 | + Console.WriteLine("ID token is malformed or invalid"); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + Console.WriteLine($"Failed to verify ID token: {ex.Message}"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // [END service_error_code] |
| 69 | + } |
| 70 | + |
| 71 | + internal static async Task PlatformErrorCode(string deviceToken) |
| 72 | + { |
| 73 | + // [START platform_error_code] |
| 74 | + var notification = CreateNotification(deviceToken); |
| 75 | + try |
| 76 | + { |
| 77 | + await FirebaseMessaging.DefaultInstance.SendAsync(notification); |
| 78 | + } |
| 79 | + catch (FirebaseMessagingException ex) |
| 80 | + { |
| 81 | + // All exceptions contain a platform-level error code. Applications can inspect |
| 82 | + // both the platform-level error code and any service-level error codes when |
| 83 | + // implementing error handling logic. |
| 84 | + if (ex.MessagingErrorCode == MessagingErrorCode.Unregistered) |
| 85 | + { |
| 86 | + // Service-level error code |
| 87 | + Console.WriteLine("App instance has been unregistered"); |
| 88 | + RemoveTokenFromDatabase(deviceToken); |
| 89 | + } |
| 90 | + else if (ex.ErrorCode == ErrorCode.Unavailable) |
| 91 | + { |
| 92 | + // Platform-level error code |
| 93 | + Console.WriteLine("FCM service is temporarily unavailable"); |
| 94 | + ScheduleForRetry(notification, TimeSpan.FromHours(1)); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + Console.WriteLine($"Failed to send notification: {ex.Message}"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // [END platform_error_code] |
| 103 | + } |
| 104 | + |
| 105 | + internal static async Task HttpResponse(string deviceToken) |
| 106 | + { |
| 107 | + // [START http_response] |
| 108 | + var notification = CreateNotification(deviceToken); |
| 109 | + try |
| 110 | + { |
| 111 | + await FirebaseMessaging.DefaultInstance.SendAsync(notification); |
| 112 | + } |
| 113 | + catch (FirebaseMessagingException ex) |
| 114 | + { |
| 115 | + // If the exception was caused by a backend service error, applications can |
| 116 | + // inspect the original error response received from the backend service to |
| 117 | + // implement more advanced error handling behavior. |
| 118 | + var response = ex.HttpResponse; |
| 119 | + if (response != null) |
| 120 | + { |
| 121 | + Console.WriteLine($"FCM service responded with HTTP {response.StatusCode}"); |
| 122 | + foreach (var entry in response.Headers) |
| 123 | + { |
| 124 | + Console.WriteLine($">>> {entry.Key}: {entry.Value}"); |
| 125 | + } |
| 126 | + |
| 127 | + var body = await response.Content.ReadAsStringAsync(); |
| 128 | + Console.WriteLine(">>>"); |
| 129 | + Console.WriteLine($">>> {body}"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // [END http_response] |
| 134 | + } |
| 135 | + |
| 136 | + private static void PerformPrivilegedOperation(string uid) { } |
| 137 | + |
| 138 | + private static Message CreateNotification(string deviceToken) |
| 139 | + { |
| 140 | + return new Message() |
| 141 | + { |
| 142 | + Token = deviceToken, |
| 143 | + Notification = new Notification() |
| 144 | + { |
| 145 | + Title = "Test notification", |
| 146 | + }, |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + private static void RemoveTokenFromDatabase(string deviceToken) { } |
| 151 | + |
| 152 | + private static void ScheduleForRetry(Message message, TimeSpan waitTime) { } |
| 153 | + } |
| 154 | +} |
0 commit comments