diff --git a/docs/CreateNotificationSuccessResponse.md b/docs/CreateNotificationSuccessResponse.md index 6da3e5f..f889229 100644 --- a/docs/CreateNotificationSuccessResponse.md +++ b/docs/CreateNotificationSuccessResponse.md @@ -4,7 +4,7 @@ | Name | Type | Description | Notes | | ---- | ---- | ----------- | ----- | -| **id** | **String** | Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). | [optional] | +| **id** | **String** | Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). All OneSignal server SDKs expose message-sent / message-not-sent narrowing helpers (named idiomatically per language — e.g. `isMessageSent`, `is_message_sent`, `message_sent?`); prefer them over comparing `id` directly. | [optional] | | **external_id** | **String** | Optional correlation / idempotency-related value from the API response. This is not the end-user External ID used for targeting recipients (that lives under `include_aliases.external_id`). | [optional] | | **errors** | **Object** | Polymorphic field: may be an array of human-readable strings and/or an object (for example with `invalid_aliases`, `invalid_external_user_ids`, or `invalid_player_ids`) depending on the API response; HTTP may still be 200 with partial success. Typed SDKs model this loosely so both shapes deserialize. | [optional] | diff --git a/lib/onesignal/helpers.rb b/lib/onesignal/helpers.rb index ac8d7a9..f6f7ab3 100644 --- a/lib/onesignal/helpers.rb +++ b/lib/onesignal/helpers.rb @@ -54,6 +54,30 @@ def self.create_notification_with_retry(api, notification, max_retries: 3, base_ end end + # Whether a POST /notifications 200 response is the "message sent" branch. + # + # POST /notifications returns 200 in two cases that share the + # CreateNotificationSuccessResponse shape: a notification was created + # (non-empty +id+), or none was (empty +id+, with +errors+ carrying the + # reason). Prefer this guard over inspecting +id+ directly. + # + # @param response [CreateNotificationSuccessResponse] + # @return [Boolean] true when a notification was created + def self.message_sent?(response) + id = response.respond_to?(:id) ? response.id : nil + id.is_a?(String) && !id.empty? + end + + # Whether a POST /notifications 200 response is the "message not sent" + # branch -- no notification was created (+id+ absent or empty); inspect + # +errors+ for why. + # + # @param response [CreateNotificationSuccessResponse] + # @return [Boolean] true when no notification was created + def self.message_not_sent?(response) + !message_sent?(response) + end + def self.header_value(headers, name) return nil unless headers.respond_to?(:each_pair) diff --git a/lib/onesignal/models/create_notification_success_response.rb b/lib/onesignal/models/create_notification_success_response.rb index 72c7f40..3b9e83c 100644 --- a/lib/onesignal/models/create_notification_success_response.rb +++ b/lib/onesignal/models/create_notification_success_response.rb @@ -15,7 +15,7 @@ module OneSignal class CreateNotificationSuccessResponse - # Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). + # Notification identifier when the request created a notification. An empty string means no notification was created; read `errors` for details (HTTP may still be 200). All OneSignal server SDKs expose message-sent / message-not-sent narrowing helpers (named idiomatically per language — e.g. `isMessageSent`, `is_message_sent`, `message_sent?`); prefer them over comparing `id` directly. attr_accessor :id # Optional correlation / idempotency-related value from the API response. This is not the end-user External ID used for targeting recipients (that lives under `include_aliases.external_id`).