|
| 1 | +// Copyright 2019, 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.Collections.Generic; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using FirebaseAdmin.Messaging; |
| 19 | + |
| 20 | +namespace FirebaseAdmin.Snippets |
| 21 | +{ |
| 22 | + internal class FirebaseMessagingSnippets |
| 23 | + { |
| 24 | + internal static async Task SendToTokenAsync() |
| 25 | + { |
| 26 | + // [START send_to_token] |
| 27 | + // This registration token comes from the client FCM SDKs. |
| 28 | + var registrationToken = "YOUR_REGISTRATION_TOKEN"; |
| 29 | + |
| 30 | + // See documentation on defining a message payload. |
| 31 | + var message = new Message() |
| 32 | + { |
| 33 | + Data = new Dictionary<string, string>() |
| 34 | + { |
| 35 | + { "score", "850" }, |
| 36 | + { "time", "2:45" }, |
| 37 | + }, |
| 38 | + Token = registrationToken, |
| 39 | + }; |
| 40 | + |
| 41 | + // Send a message to the device corresponding to the provided |
| 42 | + // registration token. |
| 43 | + string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); |
| 44 | + // Response is a message ID string. |
| 45 | + Console.WriteLine("Successfully sent message: " + response); |
| 46 | + // [END send_to_token] |
| 47 | + } |
| 48 | + |
| 49 | + internal static async Task SendToTopicAsync() |
| 50 | + { |
| 51 | + // [START send_to_topic] |
| 52 | + // The topic name can be optionally prefixed with "/topics/". |
| 53 | + var topic = "highScores"; |
| 54 | + |
| 55 | + // See documentation on defining a message payload. |
| 56 | + var message = new Message() |
| 57 | + { |
| 58 | + Data = new Dictionary<string, string>() |
| 59 | + { |
| 60 | + { "score", "850" }, |
| 61 | + { "time", "2:45" }, |
| 62 | + }, |
| 63 | + Topic = topic, |
| 64 | + }; |
| 65 | + |
| 66 | + // Send a message to the devices subscribed to the provided topic. |
| 67 | + string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); |
| 68 | + // Response is a message ID string. |
| 69 | + Console.WriteLine("Successfully sent message: " + response); |
| 70 | + // [END send_to_topic] |
| 71 | + } |
| 72 | + |
| 73 | + internal static async Task SendToConditionAsync() |
| 74 | + { |
| 75 | + // [START send_to_condition] |
| 76 | + // Define a condition which will send to devices which are subscribed |
| 77 | + // to either the Google stock or the tech industry topics. |
| 78 | + var condition = "'stock-GOOG' in topics || 'industry-tech' in topics"; |
| 79 | + |
| 80 | + // See documentation on defining a message payload. |
| 81 | + var message = new Message() |
| 82 | + { |
| 83 | + Notification = new Notification() |
| 84 | + { |
| 85 | + Title = "$GOOG up 1.43% on the day", |
| 86 | + Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", |
| 87 | + }, |
| 88 | + Condition = condition, |
| 89 | + }; |
| 90 | + |
| 91 | + // Send a message to devices subscribed to the combination of topics |
| 92 | + // specified by the provided condition. |
| 93 | + string response = await FirebaseMessaging.DefaultInstance.SendAsync(message); |
| 94 | + // Response is a message ID string. |
| 95 | + Console.WriteLine("Successfully sent message: " + response); |
| 96 | + // [END send_to_condition] |
| 97 | + } |
| 98 | + |
| 99 | + internal static async Task SendDryRunAsync() |
| 100 | + { |
| 101 | + var message = new Message() |
| 102 | + { |
| 103 | + Data = new Dictionary<string, string>() |
| 104 | + { |
| 105 | + { "score", "850" }, |
| 106 | + { "time", "2:45" }, |
| 107 | + }, |
| 108 | + Token = "token", |
| 109 | + }; |
| 110 | + |
| 111 | + // [START send_dry_run] |
| 112 | + // Send a message in the dry run mode. |
| 113 | + string response = await FirebaseMessaging.DefaultInstance.SendAsync( |
| 114 | + message, dryRun: true); |
| 115 | + // Response is a message ID string. |
| 116 | + Console.WriteLine("Dry run successful: " + response); |
| 117 | + // [END send_dry_run] |
| 118 | + } |
| 119 | + |
| 120 | + internal static Message CreateAndroidMessage() |
| 121 | + { |
| 122 | + // [START android_message] |
| 123 | + var message = new Message |
| 124 | + { |
| 125 | + Android = new AndroidConfig() |
| 126 | + { |
| 127 | + TimeToLive = TimeSpan.FromHours(1), |
| 128 | + Priority = Priority.Normal, |
| 129 | + Notification = new AndroidNotification() |
| 130 | + { |
| 131 | + Title = "$GOOG up 1.43% on the day", |
| 132 | + Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", |
| 133 | + Icon = "stock_ticker_update", |
| 134 | + Color = "#f45342", |
| 135 | + }, |
| 136 | + }, |
| 137 | + Topic = "industry-tech", |
| 138 | + }; |
| 139 | + // [END android_message] |
| 140 | + return message; |
| 141 | + } |
| 142 | + |
| 143 | + internal static Message CreateAPNSMessage() |
| 144 | + { |
| 145 | + // [START apns_message] |
| 146 | + var message = new Message |
| 147 | + { |
| 148 | + Apns = new ApnsConfig() |
| 149 | + { |
| 150 | + Headers = new Dictionary<string, string>() |
| 151 | + { |
| 152 | + { "apns-priority", "10" }, |
| 153 | + }, |
| 154 | + Aps = new Aps() |
| 155 | + { |
| 156 | + Alert = new ApsAlert() |
| 157 | + { |
| 158 | + Title = "$GOOG up 1.43% on the day", |
| 159 | + Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% " |
| 160 | + + "on the day.", |
| 161 | + }, |
| 162 | + Badge = 42, |
| 163 | + }, |
| 164 | + }, |
| 165 | + Topic = "industry-tech", |
| 166 | + }; |
| 167 | + // [END apns_message] |
| 168 | + return message; |
| 169 | + } |
| 170 | + |
| 171 | + internal static Message CreateWebpushMessage() |
| 172 | + { |
| 173 | + // [START webpush_message] |
| 174 | + var message = new Message |
| 175 | + { |
| 176 | + Webpush = new WebpushConfig() |
| 177 | + { |
| 178 | + Notification = new WebpushNotification() |
| 179 | + { |
| 180 | + Title = "$GOOG up 1.43% on the day", |
| 181 | + Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", |
| 182 | + Icon = "https://my-server/icon.png", |
| 183 | + }, |
| 184 | + }, |
| 185 | + Topic = "industry-tech", |
| 186 | + }; |
| 187 | + // [END webpush_message] |
| 188 | + return message; |
| 189 | + } |
| 190 | + |
| 191 | + internal static Message CreateMultiPlatformsMessage() |
| 192 | + { |
| 193 | + // [START multi_platforms_message] |
| 194 | + var message = new Message |
| 195 | + { |
| 196 | + Notification = new Notification() |
| 197 | + { |
| 198 | + Title = "$GOOG up 1.43% on the day", |
| 199 | + Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.", |
| 200 | + }, |
| 201 | + Android = new AndroidConfig() |
| 202 | + { |
| 203 | + TimeToLive = TimeSpan.FromHours(1), |
| 204 | + Notification = new AndroidNotification() |
| 205 | + { |
| 206 | + Icon = "stock_ticker_update", |
| 207 | + Color = "#f45342", |
| 208 | + }, |
| 209 | + }, |
| 210 | + Apns = new ApnsConfig() |
| 211 | + { |
| 212 | + Aps = new Aps() |
| 213 | + { |
| 214 | + Badge = 42, |
| 215 | + }, |
| 216 | + }, |
| 217 | + Topic = "industry-tech", |
| 218 | + }; |
| 219 | + // [END multi_platforms_message] |
| 220 | + return message; |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments