8000 Move snippets into repo · scor2k/firebase-admin-python@8716238 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8716238

Browse files
committed
Move snippets into repo
1 parent 176ef5f commit 8716238

File tree

4 files changed

+244
-1
lines changed

4 files changed

+244
-1
lines changed

lint.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ SKIP_FOR_TESTS="redefined-outer-name,protected-access,missing-docstring,too-many
3535

3636
if [[ "$#" -eq 1 && "$1" = "all" ]]
3737
then
38-
CHECK_ALL=true
38+
CHECK_ALL=true
3939
elif [[ "$#" -eq 0 ]]
4040
then
4141
CHECK_ALL=false
@@ -49,8 +49,10 @@ then
4949
lintAllFiles "firebase_admin" ""
5050
lintAllFiles "tests" "$SKIP_FOR_TESTS"
5151
lintAllFiles "integration" "$SKIP_FOR_TESTS"
52+
lintAllFiles "snippets" "$SKIP_FOR_TESTS"
5253
else
5354
lintChangedFiles "firebase_admin" ""
5455
lintChangedFiles "tests" "$SKIP_FOR_TESTS"
5556
lintChangedFiles "integration" "$SKIP_FOR_TESTS"
57+
lintChangedFiles "snippets" "$SKIP_FOR_TESTS"
5658
fi

snippets/__init__.py

Whitespace-only changes.

snippets/cloud_messaging.py

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#
2+
# Copyright 2018 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import datetime
18+
19+
from firebase_admin import messaging
20+
21+
def send_to_token():
22+
# [START send_to_token]
23+
# This registration token comes from the client FCM SDKs.
24+
registration_token = 'YOUR_REGISTRATION_TOKEN'
25+
26+
# See documentation on defining a message payload.
27+
message = messaging.Message(
28+
data={
29+
'score': '850',
30+
'time': '2:45',
31+
},
32+
token=registration_token,
33+
)
34+
35+
# Send a message to the device corresponding to the provided
36+
# registration token.
37+
response = messaging.send(message)
38+
# Response is a message ID string.
39+
print('Successfully sent message:', response)
40+
# [END send_to_token]
41+
42+
def send_to_topic():
43+
# [START send_to_topic]
44+
# The topic name can be optionally prefixed with "/topics/".
45+
topic = 'highScores'
46+
47+
# See documentation on defining a message payload.
48+
message = messaging.Message(
49+
data={
50+
'score': '850',
51+
'time': '2:45',
52+
},
53+
topic=topic,
54+
)
55+
56+
# Send a message to the devices subscribed to the provided topic.
57+
response = messaging.send(message)
58+
# Response is a message ID string.
59+
print('Successfully sent message:', response)
60+
# [END send_to_topic]
61+
62+
def send_to_condition():
63+
# [START send_to_condition]
64+
# Define a condition which will send to devices which are subscribed
65+
# to either the Google stock or the tech industry topics.
66+
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"
67+
68+
# See documentation on defining a message payload.
69+
message = messaging.Message(
70+
notification=messaging.Notification(
71+
title='$GOOG up 1.43% on the day',
72+
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
73+
),
74+
condition=condition,
75+
)
76+
77+
# Send a message to devices subscribed to the combination of topics
78+
# specified by the provided condition.
79+
response = messaging.send(message)
80+
# Response is a message ID string.
81+
print('Successfully sent message:', response)
82+
# [END send_to_condition]
83+
84+
def send_dry_run():
85+
message = messaging.Message(
86+
data={
87+
'score': '850',
88+
'time': '2:45',
89+
},
90+
token='token',
91+
)
92+
93+
# [START send_dry_run]
94+
# Send a message in the dry run mode.
95+
response = messaging.send(message, dry_run=True)
96+
# Response is a message ID string.
97+
print('Dry run successful:', response)
98+
# [END send_dry_run]
99+
100+
def android_message():
101+
# [START android_message]
102+
message = messaging.Message(
103+
android=messaging.AndroidConfig(
104+
ttl=datetime.timedelta(seconds=3600),
105+
priority='normal',
106+
notification=messaging.AndroidNotification(
107+
title='$GOOG up 1.43% on the day',
108+
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
109+
icon='stock_ticker_update',
110+
color='#f45342'
111+
),
112+
),
113+
topic='industry-tech',
114+
)
115+
# [END android_message]
116+
return message
117+
118+
def apns_message():
119+
# [START apns_message]
120+
message = messaging.Message(
121+
apns=messaging.APNSConfig(
122+
headers={'apns-priority': '10'},
123+
payload=messaging.APNSPayload(
124+
aps=messaging.Aps(
125+
alert=messaging.ApsAlert(
126+
title='$GOOG up 1.43% on the day',
127+
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
128+
),
129+
badge=42,
130+
),
131+
),
132+
),
133+
topic='industry-tech',
134+
)
135+
# [END apns_message]
136+
return message
137+
138+
def webpush_message():
139+
# [START webpush_message]
140+
message = messaging.Message(
141+
webpush=messaging.WebpushConfig(
142+
notification=messaging.WebpushNotification(
143+
title='$GOOG up 1.43% on the day',
144+
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
145+
icon='https://my-server/icon.png',
146+
),
147+
),
148+
topic='industry-tech',
149+
)
150+
# [END webpush_message]
151+
return message
152+
153+
def all_platforms_message():
154+
# [START multi_platforms_message]
155+
message = messaging.Message(
156+
notification=messaging.Notification(
157+
title='$GOOG up 1.43% on the day',
158+
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
159+
),
160+
android=messaging.AndroidConfig(
161+
ttl=datetime.timedelta(seconds=3600),
162+
priority='normal',
163+
notification=messaging.AndroidNotification(
164+
icon='stock_ticker_update',
165+
color='#f45342'
166+
),
167+
),
168+
apns=messaging.APNSConfig(
169+
payload=messaging.APNSPayload(
170+
aps=messaging.Aps(badge=42),
171+
),
172+
),
173+
topic='industry-tech',
174+
)
175+
# [END multi_platforms_message]
176+
return message
177+
178+
def subscribe_to_topic():
179+
topic = 'highScores'
180+
# [START subscribe]
181+
# These registration tokens come from the client FCM SDKs.
182+
registration_tokens = [
183+
'YOUR_REGISTRATION_TOKEN_1',
184+
# ...
185+
'YOUR_REGISTRATION_TOKEN_n',
186+
]
187+
188+
# Subscribe the devices corresponding to the registration tokens to the
189+
# topic.
190+
response = messaging.subscribe_to_topic(registration_tokens, topic)
191+
# See the TopicManagementResponse reference documentation
192+
# for the contents of response.
193+
print(response.success_count, 'tokens were subscribed successfully')
194+
# [END subscribe]
195+
196+
def unsubscribe_from_topic():
197+
topic = 'highScores'
198+
# [START unsubscribe]
199+
# These registration tokens come from the client FCM SDKs.
200+
registration_tokens = [
201+
'YOUR_REGISTRATION_TOKEN_1',
202+
# ...
203+
'YOUR_REGISTRATION_TOKEN_n',
204+
]
205+
206+
# Unubscribe the devices corresponding to the registration tokens from the
207+
# topic.
208+
response = messaging.unsubscribe_from_topic(registration_tokens, topic)
209+
# See the TopicManagementResponse reference documentation
210+
# for the contents of response.
211+
print(response.success_count, 'tokens were unsubscribed successfully')
212+
# [END unsubscribe]

snippets/get_service_account_tokens.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Copyright 2017 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# [START get_service_account_tokens]
18+
from firebase_admin import credentials
19+
20+
cred = credentials.Certificate('path/to/serviceAccountKey.json')
21+
access_token_info = cred.get_access_token()
22+
23+
access_token = access_token_info.access_token
24+
expiration_time = access_token_info.expiry
25+
# Attach access_token to HTTPS request in the "Authorization: Bearer" header
26+
# After expiration_time, you must generate a new access token
27+
# [END get_service_account_tokens]
28+
29+
print('The access token {} expires at {}'.format(access_token, expiration_time))

0 commit comments

Comments
 (0)
0