Skip to content

Instantly share code, notes, and snippets.

@MosheBerman
Created September 21, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MosheBerman/8089897b6390d55c2530c018c81330a5 to your computer and use it in GitHub Desktop.
Save MosheBerman/8089897b6390d55c2530c018c81330a5 to your computer and use it in GitHub Desktop.
A way to send the best and worst of classical literature over APNs.
import requests
import json
url = "https://fcm.googleapis.com/fcm/send"
project_key = "token"
def send(title, text):
headers = {
"Authorization": "key={}".format(project_key),
"Content-Type": "application/json"
}
payload = {
'registration_ids': [
"idsx"
],
'notification': {
"title": title,
"text": text
},
"priority" : "high"
}
print("---Sending---\n\nheaders: {}\n\nbody: {}".format(json.dumps(headers, indent=4), json.dumps(payload, indent=4)))
response = requests.post(url, data=json.dumps(payload), headers=headers)
print("\n\n---Response---\n\nJSON: {}\n\n".format(json.dumps(response.json(), indent=4)))
send("Hello World!", "High Priority")
novel = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way - in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
harry = "Nearly ten years had passed since the Dursleys had woken up to find their nephew on the front step, but Privet Drive had hardly changed at all. The sun rose on the same tidy front gardens and lit up the brass number four on the Dursleys' front door; it crept into their living room, which was almost exactly the same as it had been on the night when Mr. Dursley had seen that fateful news report about the owls. Only the photographs on the mantelpiece really showed how much time had passed. Ten years ago, there had been lots of pictures of what looked like a large pink beach ball wearing different-colored bonnets - but Dudley Dursley was no longer a baby, and now the photographs showed a large blond boy riding his first bicycle, on a carousel at the fair, playing a computer game with his father, being hugged and kissed by his mother. The room held no sign at all that another boy lived in the house, too."
def push_story(title, story, n=100):
novel_parts = [story[i:i+n] for i in range(0, len(story), n)]
for part in novel_parts:
send(title, part)
push_story("Harry Potter and the Sorcerer's Stone", harry)
push_story("A Tale Of Two Cities", novel)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment