Skip to content

Commit 3998996

Browse files
author
Camille Fabreguettes
committed
Delegate APNS calls to PyAPNs2.
1 parent 7a83ec2 commit 3998996

File tree

14 files changed

+293
-435
lines changed

14 files changed

+293
-435
lines changed

README.rst

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ Edit your settings.py file:
4646
"FCM_API_KEY": "[your api key]",
4747
"GCM_API_KEY": "[your api key]",
4848
"APNS_CERTIFICATE": "/path/to/your/certificate.pem",
49+
"APNS_TOPIC": "com.example.push_test",
4950
"WNS_PACKAGE_SECURITY_ID": "[your package security id, e.g: 'ms-app://e-3-4-6234...']",
5051
"WNS_SECRET_KEY": "[your app secret key, e.g.: 'KDiejnLKDUWodsjmewuSZkk']",
5152
}
5253
5354
.. note::
54-
If you are planning on running your project with ``DEBUG=True``, then make sure you have set the
55+
If you are planning on running your project with ``APNS_USE_SANDBOX=True``, then make sure you have set the
5556
*development* certificate as your ``APNS_CERTIFICATE``. Otherwise the app will not be able to connect to the correct host. See settings_ for details.
5657

5758
You can learn more about APNS certificates `here <https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html>`_.
@@ -71,12 +72,8 @@ For WNS, you need both the ``WNS_PACKAGE_SECURITY_KEY`` and the ``WNS_SECRET_KEY
7172
**APNS settings**
7273

7374
- ``APNS_CERTIFICATE``: Absolute path to your APNS certificate file. Certificates with passphrases are not supported.
74-
- ``APNS_CA_CERTIFICATES``: Absolute path to a CA certificates file for APNS. Optional - do not set if not needed. Defaults to None.
75-
- ``APNS_HOST``: The hostname used for the APNS sockets.
76-
- When ``DEBUG=True``, this defaults to ``gateway.sandbox.push.apple.com``.
77-
- When ``DEBUG=False``, this defaults to ``gateway.push.apple.com``.
78-
- ``APNS_PORT``: The port used along with APNS_HOST. Defaults to 2195.
79-
- ``APNS_ERROR_TIMEOUT``: The timeout on APNS sockets.
75+
- ``APNS_USE_ALTERNATIVE_PORT``: Use port 2197 for APNS, instead of default port 443.
76+
- ``APNS_USE_SANDBOX``: Use 'api.development.push.apple.com', instead of default host 'api.push.apple.com'.
8077

8178
**FCM/GCM settings**
8279

@@ -206,23 +203,6 @@ Note: gcm_send_bulk_message must be used when sending messages to topic subscrib
206203
207204
Reference: `FCM Documentation <https://firebase.google.com/docs/cloud-messaging/android/topic-messaging>`_
208205

209-
Administration
210-
--------------
211-
212-
APNS devices which are not receiving push notifications can be set to inactive by two methods. The web admin interface for
213-
APNS devices has a "prune devices" option. Any selected devices which are not receiving notifications will be set to inactive [1]_.
214-
There is also a management command to prune all devices failing to receive notifications:
215-
216-
.. code-block:: shell
217-
218-
$ python manage.py prune_devices
219-
220-
This removes all devices which are not receiving notifications.
221-
222-
For more information, please refer to the APNS feedback service_.
223-
224-
.. _service: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
225-
226206
Exceptions
227207
----------
228208

push_notifications/admin.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from django.apps import apps
22
from django.contrib import admin, messages
33
from django.utils.translation import ugettext_lazy as _
4-
from .apns import APNS_ERROR_MESSAGES, APNSServerError
4+
from .apns import APNSServerError
55
from .gcm import GCMError
66
from .models import APNSDevice, GCMDevice, WNSDevice
77
from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
8-
from .utils import get_expired_tokens
98

109
User = apps.get_model(*SETTINGS["USER_MODEL"].split("."))
1110

1211

1312
class DeviceAdmin(admin.ModelAdmin):
1413
list_display = ("__str__", "device_id", "user", "active", "date_created")
1514
list_filter = ("active",)
16-
actions = ("send_message", "send_bulk_message", "prune_devices", "enable", "disable")
15+
actions = ("send_message", "send_bulk_message", "enable", "disable")
1716
raw_id_fields = ("user",)
1817

1918
if hasattr(User, "USERNAME_FIELD"):
@@ -40,7 +39,7 @@ def send_messages(self, request, queryset, bulk=False):
4039
except GCMError as e:
4140
errors.append(str(e))
4241
except APNSServerError as e:
43-
errors.append(APNS_ERROR_MESSAGES[e.status])
42+
errors.append(e.status)
4443

4544
if bulk:
4645
break
@@ -79,19 +78,6 @@ def disable(self, request, queryset):
7978

8079
disable.short_description = _("Disable selected devices")
8180

82-
def prune_devices(self, request, queryset):
83-
# Note that when get_expired_tokens() is called, Apple's
84-
# feedback service resets, so, calling it again won't return
85-
# the device again (unless a message is sent to it again). So,
86-
# if the user doesn't select all the devices for pruning, we
87-
# could very easily leave an expired device as active. Maybe
88-
# this is just a bad API.
89-
expired = get_expired_tokens()
90-
devices = queryset.filter(registration_id__in=expired)
91-
for d in devices:
92-
d.active = False
93-
d.save()
94-
9581

9682
class GCMDeviceAdmin(DeviceAdmin):
9783
list_display = (

0 commit comments

Comments
 (0)