一个即插即用的包,用于处理ZeroPush和城市飞艇等服务的推送设备和推送通知
django-push-notifications-manager的Python项目详细描述
此包使支持推送通知变得容易。它工作 以及第三方服务,如ZeroPush。
通过链接这些设备,可以轻松地添加对推送设备的权限 通知设置注册新的推送设备时,可以添加 自定义权限。
安装
要在计算机上安装软件包,可以运行pip安装:
pip install django-push-notifications-manager
配置
首先,您必须将push_notifications添加到已安装的应用程序:
INSTALLED_APPS=(...'push_notifications',)
要设置包,必须添加DJANGO_PUSH_NOTIFICATIONS 进入您的设置:
defSettings(Configuration):# ...DJANGO_PUSH_NOTIFICATIONS={'SERVICE':'push_notifications.services.zeropush.ZeroPushService','AUTH_TOKEN':'123123123'}
可以将SERVICE键设置为您使用的服务类型。在这个 示例我们使用ZeroPushService使用ZeroPush服务,我们 必须将AUTH_TOKEN添加到Configuration
注意:现在,只有ZeroPush可用。更多的服务将 很快就有空。
用法
注册设备
要注册新设备,可以使用register_push_device方法 在utils:
frompush_notifications.utilsfromregister_push_devicetoken="<The device token>"register_push_device(user,token)
您还可以将通知权限直接传递给 register_push_device方法:
register_push_device(user,token,['likes','comments'])
添加权限
要向推送设备添加通知权限,可以使用 add_permission方法在device对象上:
device.add_permission('likes')
或添加多个权限
device.add_permissions(['likes','comments'])
添加用户拥有的设备的所有权限
user.push_devices.add_permissions(['likes','comments'])
删除权限
要删除通知权限,可以使用remove_permissiondevice对象上的方法:
device.remove_permissions('likes')
或删除多个权限
device.remove_permissions(['likes','comments'])
删除用户拥有的设备的所有权限。
user.push_devices.remove_permissions(['likes','comments'])
发送通知
要向特定权限组发送通知,可以调用 send_push_notification在utils:
frompush_notificationsimportsend_push_notificationfromdatetimeimporttimedeltasend_push_notification('likes','This is the message',sound="annoyingSound.mp3",badge_number=1info={"extra":"payload","in":"notification"},expiry=timedelta(days=30))
说明
send_push_notification(notify_type, message, **kwargs)
kwargs-sound:发送 通知-badge_number:必须是 显示的(仅限iOS)-info:随 通知-expiry:通知的到期日接受 TimeDelta和DateTime对象
向一个设备发送通知
要向特定设备发送通知,可以使用 send_push_notification在device对象上
device.send_push_notification('likes','This is the message',sound="annoyingSound.mp3",badge_number=1info={"extra":"payload","in":"notification"},expiry=timedelta(days=30))
它接受与全局^{tt17}相同的参数$ 在utils中。
向一个用户发送通知
要向用户及其所有设备发送通知,可以使用 send_push_notification在用户的push_devices名称上 对象:
user.push_devices.send_push_notification('likes','This is the message',sound="annoyingSound.mp3",badge_number=1info={"extra":"payload","in":"notification"},expiry=timedelta(days=30))