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 inSettings -> Keys & Idssection ofapppage inOneSignal. For theUala business local testapp it's1d2d57f0-a5e8-40e5-abda-cbe92f9b5ef4identifier- can be any random string. For exampletest_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.
Related entities in our app
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 ofOneSignalwe can have several providers for differentAppseach with their ownclient_keyandclient_secretthat can be find inOneSignalapp page inSettings -> Keys & Idssection.ExternalProviderCustomer- represents customer for specificCountryProviderholder- can beUserorCustomerexternal_customer_id-OneSignal'ssubscription id
So, to test we need to find appropriate ExternalProviderCustomer and use it as a recipient.
How to test
In general:
- Find appropriate working
ExternalProviderCustomer - 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?:
- Check that the recipient allows to receive this type of notifications (
Notifications::NotificationModule#_manager_allow_to_receive_type?) - Check that the appointment that you use allows to send push notification (
Notifications::NotificationModule#_appointment_allow_send?) - 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?:
- Check that the notices not sent for the user (
Notifications::NotificationModule#_notices_not_sent?) - Check that the venue didn't exceed the limit of notifications (
Notifications::NotificationModule#_notification_limit_not_exceeded?) - Check that the user allows this type of notification
- 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.