Updating to apns2 library for Apple Push Notifications over Http/2.

This commit is contained in:
Samuel Clay 2021-03-04 18:26:32 -05:00
parent 26fe4f82b8
commit 0c19aec2a3
2 changed files with 16 additions and 11 deletions

View file

@ -18,7 +18,9 @@ from utils.view_functions import is_true
from utils.story_functions import truncate_chars from utils.story_functions import truncate_chars
from utils import log as logging from utils import log as logging
from utils import mongoengine_fields from utils import mongoengine_fields
from vendor.apns import APNs, Payload from apns2.errors import BadDeviceToken
from apns2.client import APNsClient
from apns2.payload import Payload
from bs4 import BeautifulSoup, Tag from bs4 import BeautifulSoup, Tag
import urllib.parse import urllib.parse
@ -237,10 +239,7 @@ class MUserFeedNotification(mongo.Document):
def send_ios(self, story, user, usersub): def send_ios(self, story, user, usersub):
if not self.is_ios: return if not self.is_ios: return
apns = APNs(use_sandbox=False, apns = APNsClient('/srv/newsblur/config/certificates/aps.pem', use_sandbox=False)
cert_file='/srv/newsblur/config/certificates/aps.pem',
key_file='/srv/newsblur/config/certificates/aps.p12.pem',
enhanced=True)
tokens = MUserNotificationTokens.get_tokens_for_user(self.user_id) tokens = MUserNotificationTokens.get_tokens_for_user(self.user_id)
notification_title_only = is_true(user.profile.preference_value('notification_title_only')) notification_title_only = is_true(user.profile.preference_value('notification_title_only'))
@ -250,11 +249,7 @@ class MUserFeedNotification(mongo.Document):
image_url = story['image_urls'][0] image_url = story['image_urls'][0]
# print image_url # print image_url
def response_listener(error_response): confirmed_ios_tokens = []
logging.user(user, "~FRAPNS client get error-response: " + str(error_response))
apns.gateway_server.register_response_listener(response_listener)
for token in tokens.ios_tokens: for token in tokens.ios_tokens:
logging.user(user, '~BMStory notification by iOS: ~FY~SB%s~SN~BM~FY/~SB%s' % logging.user(user, '~BMStory notification by iOS: ~FY~SB%s~SN~BM~FY/~SB%s' %
(story['story_title'][:50], usersub.feed.feed_title[:50])) (story['story_title'][:50], usersub.feed.feed_title[:50]))
@ -267,7 +262,16 @@ class MUserFeedNotification(mongo.Document):
'story_feed_id': story['story_feed_id'], 'story_feed_id': story['story_feed_id'],
'image_url': image_url, 'image_url': image_url,
}) })
apns.gateway_server.send_notification(token, payload) try:
apns.send_notification(token, payload)
except BadDeviceToken:
logging.user(user, '~BMiOS token expired: ~FR~SB%s' % (token[:50]))
else:
confirmed_ios_tokens += token
if len(confirmed_ios_tokens) < len(tokens.ios_tokens):
tokens.ios_tokens = confirmed_ios_tokens
tokens.save()
def send_android(self, story): def send_android(self, story):
if not self.is_android: return if not self.is_android: return

View file

@ -1,5 +1,6 @@
# -e git://github.com/tweepy/tweepy.git#egg=tweepy # -e git://github.com/tweepy/tweepy.git#egg=tweepy
anyjson==0.3.3 anyjson==0.3.3
apns2==0.7.2
beautifulsoup4==4.9.3 beautifulsoup4==4.9.3
bleach==3.2.1 bleach==3.2.1
boto==2.49.0 boto==2.49.0