You can do it using something like pyAPNs for iOS devices and python-gcm
for android ones. I actually did it a while ago, and while it worked, I do
not recommend it as I faced many problems in the beginning, and I would
follow Anthony's suggestion nowadays. I will give you my code using these
modules, in case you really don't want to use an outside service.
db.define_table('push_devices',
Field('platform'),
Field('device_id_or_token')
)
def push_send(msg):
"""
Send a simple msg
"""
from apns import APNs, Payload
import os
import socket
appdir = request.folder
ios_devices = [ios.device_id_or_token for ios in
db(db.push_devices.platform
== 'ios').select(db.push_devices.device_id_or_token)]
key = os.path.join(appdir, 'YOUR_KEY_FILE.pem')
cert = os.path.join(appdir, 'YOUR_CERT_FILE.pem')
apns = APNs(use_sandbox=False, cert_file=cert, key_file=key)
payload = Payload(alert=msg)
for device in ios_devices:
try:
apns.gateway_server.send_notification(device, payload)
except socket.error as e:
# Most likely cause of a broken pipe or this token being
invalid! (maybe the user uninstalled)
apns = APNs(use_sandbox=False, cert_file=cert, key_file=key)
logger.debug('SOCKET ERROR - ' + str(e.errno) + ' deleting ' +
device)
db((db.push_devices.platform == 'ios') &
(db.push_devices.device_id_or_token
== device)).delete()
continue
from gcm import GCM
gcm = GCM('YOUR_GOOGLE_SERVER_API_KEY') # Google Server API key
android_devices = [android.device_id_or_token for android in db(db.
push_devices.platform == 'android').select(db.push_devices.
device_id_or_token)]
len_android = len(android_devices)
while android_devices:
tosend, android_devices = android_devices[:1000], android_devices[
1000:]
gcm.json_request(registration_ids=tosend, data={'message': msg})
return (len_android + len(ios_devices))
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.