Commit 17baeac4 by dgrtwo

Added README and unit tests on Push functionality. Changed functionality…

Added README and unit tests on Push functionality. Changed functionality slightly to allow channels= to be specified alongside a where= argument.
parent 52f31632
......@@ -371,6 +371,36 @@ u.save()
u.delete()
~~~~~
Push
---------------
You can also send notifications to your users using [Parse's Push functionality](https://parse.com/products/push), through the Push object:
~~~~~ {python}
from parse_rest.installation import Push
Push.message("The Giants won against the Mets 2-3.",
channels=["Giants", "Mets"])
~~~~~
This will push a message to all users subscribed to the "Giants" and "Mets" channels. Your alert can be restricted based on [Advanced Targeting](https://www.parse.com/docs/push_guide#sending-queries/REST) by specifying the `where` argument:
~~~~~ {python}
Push.message("Willie Hayes injured by own pop fly.",
channels=["Giants"], where={"injuryReports": True})
Push.message("Giants scored against the A's! It's now 2-2.",
channels=["Giants"], where={"scores": True})
~~~~~
If you wish to include more than a simple message in your notification, such as incrementing the app badge in iOS or adding a title in Android, use the `alert` method and pass the actions in a dictionary:
~~~~~ {python}
Push.alert({"alert": "The Mets scored! The game is now tied 1-1.",
"badge": "Increment", "title": "Mets Score"}, channels=["Mets"],
where={"scores": True})
~~~~~
Cloud Functions
---------------
......
......@@ -25,7 +25,13 @@ class Push(ParseResource):
@classmethod
def _send(cls, data, where=None, **kw):
if where: kw['where'] = where
if where:
kw['where'] = where
# allow channels to be specified even if "where" is as well
if "channels" in kw:
kw['where']["channels"] = kw.pop("channels")
return cls.POST('', data=data, **kw)
@classmethod
......
......@@ -17,6 +17,7 @@ from connection import register, ParseBatcher
from datatypes import GeoPoint, Object, Function
from user import User
import query
from installation import Push
try:
import settings_local
......@@ -441,6 +442,29 @@ class TestUser(unittest.TestCase):
'Failed to batch update user data: updatedAt not changed')
class TestPush(unittest.TestCase):
"""
Test Push functionality. Currently just sends the messages, ensuring they
don't lead to an error, but does not test whether the messages actually
went through and with the proper attributes (may be worthwhile to
set up such a test).
"""
def testCanMessage(self):
Push.message("Giants beat the Mets.",
channels=["Giants", "Mets"])
Push.message("Willie Hayes injured by own pop fly.",
channels=["Giants"], where={"injuryReports": True})
Push.message("Giants scored against the A's! It's now 2-2.",
channels=["Giants"], where={"scores": True})
def testCanAlert(self):
Push.alert({"alert": "The Mets scored! The game is now tied 1-1.",
"badge": "Increment", "title": "Mets Score"},
channels=["Mets"], where={"scores": True})
def run_tests():
"""Run all tests in the parse_rest package"""
tests = unittest.TestLoader().loadTestsFromNames(['parse_rest.tests'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment