> Good lord what is happening in there Previously the contents of the Web Push API payloads closely resembled the structure of JavaScript's [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification). But now that the API is open to non-browser apps, and given that there is no required coupling between contents of the payload and a Notification object, here is how I changed the payload: ```json { "access_token": "...", "preferred_locale": "en", "notification_id": "12345", "notification_type": "follow", "title": "So and so followed you", "body": "This is my bio", "icon": "https://example.com/avatar.png" } ``` The title, body and icon attributes are included as a fallback so you can construct a minimal notification if you cannot perform a network request to the API to get more data.
		
			
				
	
	
		
			39 lines
		
	
	
		
			997 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			997 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| # frozen_string_literal: true
 | |
| 
 | |
| class Web::NotificationSerializer < ActiveModel::Serializer
 | |
|   include RoutingHelper
 | |
|   include ActionView::Helpers::TextHelper
 | |
|   include ActionView::Helpers::SanitizeHelper
 | |
| 
 | |
|   attributes :access_token, :preferred_locale, :notification_id,
 | |
|              :notification_type, :icon, :title, :body
 | |
| 
 | |
|   def access_token
 | |
|     current_push_subscription.associated_access_token
 | |
|   end
 | |
| 
 | |
|   def preferred_locale
 | |
|     current_push_subscription.associated_user&.locale || I18n.default_locale
 | |
|   end
 | |
| 
 | |
|   def notification_id
 | |
|     object.id
 | |
|   end
 | |
| 
 | |
|   def notification_type
 | |
|     object.type
 | |
|   end
 | |
| 
 | |
|   def icon
 | |
|     full_asset_url(object.from_account.avatar_static_url)
 | |
|   end
 | |
| 
 | |
|   def title
 | |
|     I18n.t("notification_mailer.#{object.type}.subject", name: object.from_account.display_name.presence || object.from_account.username)
 | |
|   end
 | |
| 
 | |
|   def body
 | |
|     truncate(strip_tags(object.target_status&.spoiler_text&.presence || object.target_status&.text || object.from_account.note), length: 140)
 | |
|   end
 | |
| end
 |