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

Fix silent push #987

Merged
merged 2 commits into from
Oct 6, 2024
Merged

Fix silent push #987

merged 2 commits into from
Oct 6, 2024

Conversation

shankari
Copy link
Contributor

@shankari shankari commented Oct 6, 2024

In #980, we migrated the push code over to HTTP v1 of the FCM API.
This appeared to work then, and is still working now for some deployments

Working (nrel-commute)

Found configuration, overriding...
Activating the environment...
Run trip labeling reminder...
WARNING:root:Push configured for app gov.nrel.cims.openpath using platform firebase with token AAAAsojuOg... of length 152
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
after mapping iOS tokens, imported 0 -> processed 0
combo token map has 1 ios entries and 1 android entries
Successfully sent to...
{'success': 1, 'failure': 0, 'results': {'...': 'projects/nrel-openpath/messages/0:1728230420488714%3bcd12ae3bcd12ae'}}
Successfully sent to...
{'success': 1, 'failure': 0, 'results': {'...': 'projects/nrel-openpath/messages/1728230420536046'}}

or

Found configuration, overriding...
Activating the environment...
Run silent IOS push...
WARNING:root:Push configured for app gov.nrel.cims.openpath using platform firebase with token AAAAsojuOg... of length 152
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
after mapping iOS tokens, imported 0 -> processed 0
combo token map has 24 ios entries and 0 android entries
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Successfully sent to ...
Successfully sent to ...
{'success': 7, 'failure': 17, 'results': {}
{'ios': {'success': 7, 'failure': 17, 'results': {}}

Not working (ccebike)

Found configuration, overriding...
Activating the environment...
Run silent IOS push...
WARNING:root:Push configured for app ... using platform firebase with token ... of length 152
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
Traceback (most recent call last):
  File "/usr/src/app/bin/push/silent_ios_push.py", line 24, in <module>
    response = pnu.send_silent_notification_to_ios_with_interval(args.interval, dev=args.dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_usage.py", line 41, in send_silent_notification_to_ios_with_interval
    return __get_default_interface__().send_silent_notification(token_map, 
{}
, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 194, in send_silent_notification
    fcm_token_map = self.convert_to_fcm_if_necessary(token_map, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 137, in convert_to_fcm_if_necessary
    importedResultList = self.retrieve_fcm_tokens(unmapped_token_list, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 98, in retrieve_fcm_tokens
    importHeaders = {"Authorization": "key=%s" % self.server_auth_token,
AttributeError: 'FirebasePush' object has no attribute 'server_auth_token'
Found configuration, overriding...
Activating the environment...
Run trip labeling reminder...
WARNING:root:Push configured for app ... using platform firebase with token ... of length 152
Config file not found, returning a copy of the environment variables instead...
Traceback (most recent call last):
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
  File "/usr/src/app/bin/push/push_remind.py", line 109, in <module>
Connecting to database URL mongodb://...
    response = pnu.send_visible_notification_to_users(uuids_to_notify,
  File "/usr/src/app/emission/net/ext_service/push/notify_usage.py", line 28, in send_visible_notification_to_users
    return __get_default_interface__().send_visible_notification(token_map, title, message, json_data, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 155, in send_visible_notification
    fcm_token_map = self.convert_to_fcm_if_necessary(token_map, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 137, in convert_to_fcm_if_necessary
    importedResultList = self.retrieve_fcm_tokens(unmapped_token_list, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 98, in retrieve_fcm_tokens
    importHeaders = {"Authorization": "key=%s" % self.server_auth_token,
AttributeError: 'FirebasePush' object has no attribute 'server_auth_token'

As part of e-mission#980
(and
e-mission@5a8ca40
in particular), we initialize the firebase module with the values we read from
the config.

However, we removed the initialization of the `server_auth_token`
https://github.com/e-mission/e-mission-server/pull/980/files#diff-5a90853314b14f3bec03976fa2ba81ec657e6327b770e9b376cfeabc0544487eL24

This is no longer used to send the actual notification, but it is used to map
the FCM tokens to APNS.

Note this is working for some deployments (e.g. nrel_commute) but not for other (e.g. stage). I can confirm that I get reminder notifications on my personal android phone. I don't understand how this ever worked - before e-mission#980, we weren't using the HTTP v1 version so no notifications were sent.  After the  change, this should have prevented notifications from being sent out.

Testing done.

Before the change

```
Connecting to database URL ...
Traceback (most recent call last):
  File "/usr/src/app/bin/push/silent_ios_push.py", line 24, in <module>
    response = pnu.send_silent_notification_to_ios_with_interval(args.interval, dev=args.dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_usage.py", line 41, in send_silent_notification_to_ios_with_interval
    return __get_default_interface__().send_silent_notification(token_map, {}, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 194, in send_silent_notification
    fcm_token_map = self.convert_to_fcm_if_necessary(token_map, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 137, in convert_to_fcm_if_necessary
    importedResultList = self.retrieve_fcm_tokens(unmapped_token_list, dev)
  File "/usr/src/app/emission/net/ext_service/push/notify_interface_impl/firebase.py", line 98, in retrieve_fcm_tokens
    importHeaders = {"Authorization": "key=%s" % self.server_auth_token,
AttributeError: 'FirebasePush' object has no attribute 'server_auth_token'
```

After the change

```
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
Received invalid result for batch starting at = 0
after mapping iOS tokens, imported 0 -> processed 0
combo token map has 30 ios entries and 0 android entries
Successfully sent to ...
Successfully sent to ...
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Successfully sent to ...
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
Successfully sent to -3sW...
Found error Token not registered while sending to token ... skipping
Successfully sent to ...
```
We check to see if a user has valid trips in the past 7 days before we send
them a reminder
(e-mission@7034479).
However, all the related messages are displayed using `logging.debug`. Because
of the additional imports, logging is configured to WARNING, so they are never
displayed.

And in the case where there are no users with recent trips, this results in
insufficient logging to determine what happened.

- Changing the logging configuration location
- Changing the log level to INFO
- Bumping up some useful logs to INFO

Testing done:

Before the change

```
Found configuration, overriding...
Activating the environment...
Run trip labeling reminder...
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
WARNING:root:Push configured for app .... using platform firebase with token ... of length 152
```

After the change

```
Found configuration, overriding...
Activating the environment...
Run trip labeling reminder...
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
WARNING:root:Push configured for app ... using platform firebase with token ... of length 152
INFO:root:Successfully downloaded config with version 1 for Staging environment for testing programs only and data collection URL https://openpath-stage.nrel.gov/api/
INFO:root:No users to notify in lang en
```
@shankari shankari merged commit 593e61c into e-mission:master Oct 6, 2024
5 checks passed
@shankari
Copy link
Contributor Author

shankari commented Oct 6, 2024

We should also add some tests for retrieve_fcm_tokens, but I don't have time to do it right now

@shankari
Copy link
Contributor Author

shankari commented Oct 6, 2024

Events are now being generated on staging

Found configuration, overriding...
Activating the environment...
Run silent IOS push...
WARNING:root:Push configured for app ... using platform firebase with token ... of length 152
Config file not found, returning a copy of the environment variables instead...
Retrieved config: {'DB_HOST': '...', 'DB_RESULT_LIMIT': None}
Connecting to database URL ...
Received invalid result for batch starting at = 0
after mapping iOS tokens, imported 0 -> processed 0
combo token map has 30 ios entries and 0 android entries
...
{'success': 16, 'failure': 14, 'results': {}, 'android': {'success': 'skipped', 'failure': 'skipped', 'results': 'skipped'}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant