Skip to content

Commit 363166b

Browse files
authored
Adds send each snippets. (#891)
1 parent e4aff7e commit 363166b

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

snippets/messaging/cloud_messaging.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,29 @@ def send_all():
245245
# [END send_all]
246246

247247

248+
def send_each():
249+
registration_token = 'YOUR_REGISTRATION_TOKEN'
250+
# [START send_each]
251+
# Create a list containing up to 500 messages.
252+
messages = [
253+
messaging.Message(
254+
notification=messaging.Notification('Price drop', '5% off all electronics'),
255+
token=registration_token,
256+
),
257+
# ...
258+
messaging.Message(
259+
notification=messaging.Notification('Price drop', '2% off all books'),
260+
topic='readers-club',
261+
),
262+
]
263+
264+
response = messaging.send_each(messages)
265+
# See the BatchResponse reference documentation
266+
# for the contents of response.
267+
print('{0} messages were sent successfully'.format(response.success_count))
268+
# [END send_each]
269+
270+
248271
def send_multicast():
249272
# [START send_multicast]
250273
# Create a list containing up to 500 registration tokens.
@@ -289,3 +312,28 @@ def send_multicast_and_handle_errors():
289312
failed_tokens.append(registration_tokens[idx])
290313
print('List of tokens that caused failures: {0}'.format(failed_tokens))
291314
# [END send_multicast_error]
315+
316+
317+
def send_each_for_multicast_and_handle_errors():
318+
# [START send_each_for_multicast_error]
319+
# These registration tokens come from the client FCM SDKs.
320+
registration_tokens = [
321+
'YOUR_REGISTRATION_TOKEN_1',
322+
# ...
323+
'YOUR_REGISTRATION_TOKEN_N',
324+
]
325+
326+
message = messaging.MulticastMessage(
327+
data={'score': '850', 'time': '2:45'},
328+
tokens=registration_tokens,
329+
)
330+
response = messaging.send_each_for_multicast(message)
331+
if response.failure_count > 0:
332+
responses = response.responses
333+
failed_tokens = []
334+
for idx, resp in enumerate(responses):
335+
if not resp.success:
336+
# The order of responses corresponds to the order of the registration tokens.
337+
failed_tokens.append(registration_tokens[idx])
338+
print('List of tokens that caused failures: {0}'.format(failed_tokens))
339+
# [END send_each_for_multicast_error]

0 commit comments

Comments
 (0)