Skip to main content

How to test push notifications in dev environment

OneSignal​

For sending push notifications we use OneSignal service (https://onesignal.com/). You can check workflow here. We have multiple Apps in the OneSignal service and for testing purposes we usually use Uala business local test where you can check tab Audience -> Test subsriptions and find there several subscribers that we can use. Most of them are pretty old and not working anymore. In case when you don't see any working subscriptions you can create it via API call:

curl --location 'https://onesignal.com/api/v1/players' \
--header 'content-type: application/json' \
--data '{
"app_id": <app_id>,
"identifier": <identifier>,
"device_model": "Electron",
"device_type": 1,
"language": "it"
}'

where:

  • app_id - can be found in Settings -> Keys & Ids section of app page in OneSignal. For the Uala business local test app it's 1d2d57f0-a5e8-40e5-abda-cbe92f9b5ef4
  • identifier - can be any random string. For example test_subscription

So, basic example for test app will be:

curl --location 'https://onesignal.com/api/v1/players' \
--header 'content-type: application/json' \
--data '{
"app_id": "1d2d57f0-a5e8-40e5-abda-cbe92f9b5ef4",
"identifier": "new_test_subscription",
"device_model": "Electron",
"device_type": 1,
"language": "it"
}'

Response in this case will be:

{
"success": true,
"id": <subscription_id>
}

If OneSignal's app already has subscription with same identifier then response will be the same and will contain existing subscription_id. The subscription_id then can be used as an external_customer_id for ExternalProviderCustomer model in our app. Also, you can find this subscription in Audience -> Subscriptions tab and move it to Test subscriptions with some name. This action is only for more convenient usage of the subscription.

To map OneSignal's subscription to our models we use couple of models:

  • CountryProvider - represents third party services that can be different from country to country. In case of OneSignal we can have several providers for different Apps each with their own client_key and client_secret that can be find in OneSignal app page in Settings -> Keys & Ids section.
  • ExternalProviderCustomer - represents customer for specific CountryProvider
    • holder - can be User or Customer
    • external_customer_id - OneSignal's subscription id

So, to test we need to find appropriate ExternalProviderCustomer and use it as a recipient.

How to test​

In general:

  1. Find appropriate working ExternalProviderCustomer
  2. Check that in your environment ENV['SEND_PUSH_NOTIFICATIONS'] == 'true'

If you want to send a notification to manager (Notifications::NotificationModule#_should_send_notification_to_manager?:

  1. Check that the recipient allows to receive this type of notifications (Notifications::NotificationModule#_manager_allow_to_receive_type?)
  2. Check that the appointment that you use allows to send push notification (Notifications::NotificationModule#_appointment_allow_send?)
  3. Check that the venue that you use allows to send notifications to managers (Notifications::NotificationModule#_venue_allow_send_to_manager?)

If you want to send a notification to a user (Notifications::NotificationModule#_should_send_notification_to_user?:

  1. Check that the notices not sent for the user (Notifications::NotificationModule#_notices_not_sent?)
  2. Check that the venue didn't exceed the limit of notifications (Notifications::NotificationModule#_notification_limit_not_exceeded?)
  3. Check that the user allows this type of notification
  4. Check that the venue allows this type of notification

If everything is ok you can send a notification using Notifications::PushService that has different methods for different types of notifications.

If you created your own subscription(s) for the test, it's better to delete them after you finished.

Possible errors​

When you sending a notification you can get an error from OneSignal service. Something like: ERROR sending OneSignal push notification: HTTP Status=200 Response Body={"id":"","errors":["All included players are not subscribed"]

it means that subscription is not valid anymore and you need to find/create another one.