Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat use firebase for ios android #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ editor, for sending beautiful emails to your subscribers and
push notification support through Apple Push Notification service (APNs)
and Google Cloud Messaging (GCM).

## How to deploy
Update the code, then:
```
# be sure to be connected to Pypi (optional)
poetry config pypi-token.pypi yourToken

poetry build
poetry publish
```

## How it works

Nuntius is agnostic about your subscribers model. You can use your current
Expand Down Expand Up @@ -103,13 +113,12 @@ In order to use push notifications, `NUNTIUS_PUSH_NOTIFICATION_SETTINGS` must be
(cf. [the `django-push-notifications` documentation](https://github.com/jazzband/django-push-notifications#settings-list))
```py
NUNTIUS_PUSH_NOTIFICATIONS_SETTINGS = {
"FCM_API_KEY": "[your api key]",
"GCM_API_KEY": "[your api key]",
"APNS_CERTIFICATE": "/path/to/your/certificate.pem",
"APNS_TOPIC": "com.example.push_test",
"PLATFORM": "FCM",
"FIREBASE_APP": "[your api key]",
# ...
}
```
We are using Firebase to send notifications on Android and iOS devices.

## Advanced usage

Expand Down
2 changes: 1 addition & 1 deletion nuntius/app_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.conf import settings

CAMPAIGN_TYPE_EMAIL = "email"
Expand Down
54 changes: 26 additions & 28 deletions nuntius/utils/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from nuntius.models import PushCampaignSentStatusType
from nuntius.utils.messages import sign_url, extend_query

from firebase_admin import messaging

def get_pushing_error_classes():
try:
from push_notifications import apns, gcm
from push_notifications import gcm
except ImportError:
classes = tuple()
else:
classes = (gcm.GCMError, apns.APNSError)
classes = (gcm.FirebaseError)

return classes

Expand Down Expand Up @@ -59,33 +60,34 @@ def notification_for_event(sent_event):

return notification


def push_apns_notification(device, notification, thread_id):
try:
from push_notifications.apns import APNSServerError
except ImportError as e:
raise e
else:
try:
device.send_message(
message=notification,
thread_id=thread_id,
extra={"url": notification["url"]},
)
except APNSServerError as e:
if "Unregistered" in str(e):
device.active = False
device.save()
raise e


def push_gcm_notification(device, notification, thread_id):
device.send_message(message=None, thread_id=thread_id, extra=notification)
ttl = 259200 # equals 3 days
push_message = messaging.Message(
data={
"url": notification["url"]
},
notification=messaging.Notification(
title=notification["title"],
body=notification["body"],
image=notification["notification_icon"]
),
android=messaging.AndroidConfig(
ttl=ttl,
collapse_key=thread_id
),
apns=messaging.APNSConfig(
headers={
"apns-expiration": ttl,
"apns-collapse-id": thread_id
}
)
)
device.send_message(push_message)


def push_notification(notification, push_sent_event):
try:
from push_notifications.models import APNSDevice, GCMDevice
from push_notifications.models import GCMDevice
except ImportError:
push_sent_event.result = PushCampaignSentStatusType.ERROR
else:
Expand All @@ -94,10 +96,6 @@ def push_notification(notification, push_sent_event):
pushed_count = 0
for device in push_sent_event.devices:
try:
if isinstance(device, APNSDevice):
push_apns_notification(
device, notification, push_sent_event.campaign.id
)
if isinstance(device, GCMDevice):
push_gcm_notification(
device, notification, push_sent_event.campaign.id
Expand Down
Loading