Class: Parse::Installation

Inherits:
Object show all
Defined in:
lib/parse/model/classes/installation.rb,
lib/parse/stack/generators/templates/model_installation.rb

Overview

This class represents the data and columns contained in the standard Parse ‘_Installation` collection. This class is also responsible for managing the device tokens for mobile devices in order to use push notifications. All queries done to send pushes using Parse::Push are performed against the Installation collection. An installation object represents an instance of your app being installed on a device. These objects are used to store subscription data for installations which have subscribed to one or more push notification channels.

The default schema for Installation is as follows:

class Parse::Installation < Parse::Object
   # See Parse::Object for inherited properties...

  property :gcm_sender_id, field: :GCMSenderId
  property :app_identifier
  property :app_name
  property :app_version
  property :app_build_number
  property :badge, :integer
  property :channels, :array
  property :device_token
  property :device_token_last_modified, :integer
  property :device_type, enum: [:ios, :android, :osx, :tvos, :watchos, :web, :expo, :win, :other, :unknown, :unsupported]
  property :installation_id
  property :locale_identifier
  property :parse_version
  property :push_type
  property :time_zone, :timezone

  has_one :session, ->{ where(installation_id: i.installation_id) }, scope_only: true
end

See Also:

Constant Summary

Constants inherited from Object

Object::BUILTIN_PARSE_CLASS_NAMES, Object::IDENTIFICATION_FIELDS, Object::VALID_ACL_POLICIES

Constants included from Core::Schema

Core::Schema::DEFAULT_PUBLIC_CLP, Core::Schema::SCHEMA_READONLY_CLASSES

Constants included from Core::Describe

Core::Describe::ALL_SECTIONS, Core::Describe::CORE_FIELD_KEYS, Core::Describe::LOCAL_SECTIONS, Core::Describe::NETWORK_SECTIONS

Constants included from Core::Indexing

Core::Indexing::MAX_INDEXES_PER_COLLECTION, Core::Indexing::PARSE_MANAGED_ARRAY_FIELDS, Core::Indexing::SENSITIVE_FIELDS

Constants included from Core::SearchIndexing

Core::SearchIndexing::ALLOWED_INDEX_TYPES, Core::SearchIndexing::INDEX_NAME_PATTERN

Constants included from Core::Fetching

Core::Fetching::NON_SERIALIZABLE_IVARS

Constants included from Core::ParseReference

Core::ParseReference::OBJECT_ID_LENGTH, Core::ParseReference::SEPARATOR

Constants included from Core::FieldGuards

Core::FieldGuards::GUARD_MODES

Constants included from Properties

Properties::BASE, Properties::BASE_FIELD_MAP, Properties::BASE_KEYS, Properties::CORE_FIELDS, Properties::DELETE_OP, Properties::PROTECTED_INITIALIZE_KEYS, Properties::PROTECTED_MASS_ASSIGNMENT_KEYS, Properties::TYPES

Constants inherited from Pointer

Pointer::ATTRIBUTES, Pointer::OBJECT_ID_FORMAT

Constants inherited from Model

Model::CLASS_AUDIENCE, Model::CLASS_INSTALLATION, Model::CLASS_JOB_SCHEDULE, Model::CLASS_JOB_STATUS, Model::CLASS_PRODUCT, Model::CLASS_PUSH_STATUS, Model::CLASS_ROLE, Model::CLASS_SCHEMA, Model::CLASS_SESSION, Model::CLASS_USER, Model::ID, Model::KEY_CLASS_NAME, Model::KEY_CREATED_AT, Model::KEY_OBJECT_ID, Model::KEY_UPDATED_AT, Model::OBJECT_ID, Model::TYPE_ACL, Model::TYPE_BYTES, Model::TYPE_DATE, Model::TYPE_FIELD, Model::TYPE_FILE, Model::TYPE_GEOPOINT, Model::TYPE_NUMBER, Model::TYPE_OBJECT, Model::TYPE_POINTER, Model::TYPE_POLYGON, Model::TYPE_RELATION

Instance Attribute Summary collapse

Attributes inherited from Object

#acl, #created_at, #id, #updated_at

Attributes inherited from Pointer

#id, #parse_class

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#[], #[]=, #__type, #_resolve_acl_owner_id, #_resolve_default_acl, #acl_changed?, acl_owner_field, acl_policy, acl_policy_setting, #acl_was, #acl_will_change!, #after_create, #after_destroy, #after_save, #after_update, #after_validation, #apply_defaults!, #around_create, #around_destroy, #around_save, #around_update, #around_validation, #as_json, #autofetch_disabled?, #before_create, #before_destroy, #before_save, #before_update, #before_validation, build, #changed, #changed?, class_permissions, #clear_attribute_change!, #clear_changes!, #clear_partial_fetch_state!, default_acls, describe_access, #disable_autofetch!, #enable_autofetch!, #existed?, fetch_clp, #fetched?, #fetched_keys, #fetched_keys=, #field_was_fetched?, #filter_for_user, filter_results_for_user, #fully_fetched?, #has?, #has_selective_keys?, #initialize, #keys, master_only_class!, #nested_fetched_keys, #nested_fetched_keys=, #nested_keys_for, #new?, #parse_class, #partially_fetched?, #persisted?, pointer, #pretty, private_acl!, protect_fields, #reload!, roles_for_user, #rollback!, #run_after_create_callbacks, #run_after_delete_callbacks, #run_after_save_callbacks, #schema, set_class_access, set_clp, set_default_acl, set_default_clp, set_read_user_fields, set_write_user_fields, #twin, unlistable_class!, update_clp!, #updates, #valid?, #validate!, webhook, webhook_function

Methods included from Core::Querying

#all, #count, #count_distinct, #cursor, #distinct, #each, #find, #find_cached, #first, #last_updated, #latest, #literal_where, #newest, #oldest, #query, #scope

Methods included from Core::Schema

#_default_class_level_permissions_for_upgrade, #auto_upgrade!, #create_schema, #fetch_schema, #reset_clp!, #schema, #update_schema

Methods included from Core::Describe

#describe

Methods included from Core::Indexing

#apply_indexes!, #indexes_plan, #mongo_geo_index, #mongo_index, #mongo_index_declarations, #mongo_relation_index

Methods included from Core::SearchIndexing

#apply_search_indexes!, #mongo_search_index, #mongo_search_index_declarations, #search_indexes_plan

Methods included from Agent::MetadataDSL

#agent_description, #agent_methods, included, #property_descriptions, #property_enum_descriptions

Methods included from Core::Actions

#_deleted?, #change_requests, #changes_applied!, #changes_payload, #create, #destroy, #destroy_request, #op_add!, #op_add_relation!, #op_add_unique!, #op_destroy!, #op_increment!, #op_remove!, #op_remove_relation!, #operate_field!, #prepare_save!, #relation_change_operations, #save, #save!, #set_attributes!, #update, #update!, #update_relations, #uri_path

Methods included from Core::Fetching

#autofetch!, #fetch, #fetch!, #fetch_cache!, #fetch_json, #fetch_object, #prepare_for_dirty_tracking!

Methods included from Associations::HasMany

has_many, #relation_changes?, #relation_updates, #relations

Methods included from Associations::BelongsTo

belongs_to, #key?

Methods included from Associations::HasOne

has_one

Methods included from Core::ParseReference

format, generate_object_id, parse

Methods included from Core::FieldGuards

#apply_field_guards!

Methods included from Core::EnhancedChangeTracking

included

Methods included from Properties

#apply_attributes!, #attribute_changes?, #attribute_updates, #attributes, #attributes=, #field_map, #fields, #format_operation, #format_value

Methods inherited from Pointer

#==, #[], #[]=, #__type, #attributes, #className, #fetch, #fetch_cache!, #fetch_json, #fetch_object, #fetched?, #hash, #initialize, #json_hash, #method_missing, #pointer, #pointer?, #present?, #respond_to_missing?, #sig

Methods inherited from Model

#dirty?, find_class

Methods included from Client::Connectable

#client

Constructor Details

This class inherits a constructor from Parse::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Parse::Pointer

Instance Attribute Details

#app_build_numberString

The build number of the client application to which this installation belongs.

Returns:



70
# File 'lib/parse/model/classes/installation.rb', line 70

property :app_build_number

#app_identifierString

A unique identifier for this installation’s client application. In iOS, this is the Bundle Identifier.

Returns:



55
# File 'lib/parse/model/classes/installation.rb', line 55

property :app_identifier

#app_nameString

The display name of the client application to which this installation belongs.

Returns:



60
# File 'lib/parse/model/classes/installation.rb', line 60

property :app_name

#app_versionString

The version string of the client application to which this installation belongs.

Returns:



65
# File 'lib/parse/model/classes/installation.rb', line 65

property :app_version

#badgeInteger

A number field representing the last known application badge for iOS installations.

Returns:

  • (Integer)


75
# File 'lib/parse/model/classes/installation.rb', line 75

property :badge, :integer

#channelsArray

An array of the channels to which a device is currently subscribed. Note that channelUris (the Microsoft-generated push URIs for Windows devices) is not supported at this time.

Returns:



82
# File 'lib/parse/model/classes/installation.rb', line 82

property :channels, :array

#device_tokenString

The Apple or Google generated token used to deliver messages to the APNs or GCM push networks respectively.

Returns:



88
# File 'lib/parse/model/classes/installation.rb', line 88

property :device_token

#device_token_last_modifiedInteger

Returns number of seconds since token modified.

Returns:

  • (Integer)

    number of seconds since token modified



92
# File 'lib/parse/model/classes/installation.rb', line 92

property :device_token_last_modified, :integer

#device_typeString

The type of device: “ios”, “android”, “osx”, “tvos”, “watchos”, “web”, “expo”, “win”, “other”, “unknown”, or “unsupported”. This property is implemented as a Parse::Stack enumeration.

Returns:



99
# File 'lib/parse/model/classes/installation.rb', line 99

property :device_type, enum: [:ios, :android, :osx, :tvos, :watchos, :web, :expo, :win, :other, :unknown, :unsupported]

#gcm_sender_idString

This field only has meaning for Android installations that use the GCM push type. It is reserved for directing Parse to send pushes to this installation with an alternate GCM sender ID. This field should generally not be set unless you are uploading installation data from another push provider. If you set this field, then you must set the GCM API key corresponding to this GCM sender ID in your Parse application’s push settings.

Returns:



50
# File 'lib/parse/model/classes/installation.rb', line 50

property :gcm_sender_id, field: :GCMSenderId

#installation_idString

Universally Unique Identifier (UUID) for the device used by Parse. It must be unique across all of an app’s installations. (readonly).

Returns:



105
# File 'lib/parse/model/classes/installation.rb', line 105

property :installation_id

#locale_identifierString

The locale for this device.

Returns:



110
# File 'lib/parse/model/classes/installation.rb', line 110

property :locale_identifier

#parse_versionString

The version of the Parse SDK which this installation uses.

Returns:



115
# File 'lib/parse/model/classes/installation.rb', line 115

property :parse_version

#push_typeString

This field is reserved for directing Parse to the push delivery network to be used. If the device is registered to receive pushes via GCM, this field will be marked “gcm”. If this device is not using GCM, and is using Parse’s push notification service, it will be blank (readonly).

Returns:



123
# File 'lib/parse/model/classes/installation.rb', line 123

property :push_type

#sessionParse::Session

Returns the corresponding Session associated with this installation, if any exists. This is implemented as a has_one association to the Session class using the #installation_id.

Returns:

Version:

  • 1.7.1



136
# File 'lib/parse/model/classes/installation.rb', line 136

has_one :session, -> { where(installation_id: i.installation_id) }, scope_only: true

#time_zoneParse::TimeZone

The current time zone where the target device is located. This should be an IANA time zone identifier or a TimeZone instance.

Returns:



129
# File 'lib/parse/model/classes/installation.rb', line 129

property :time_zone, :timezone

Class Method Details

.all_channelsArray<String>

List all unique channel names across all installations.

Examples:

all_channels = Parse::Installation.all_channels
# => ["news", "sports", "weather"]

Returns:



148
149
150
# File 'lib/parse/model/classes/installation.rb', line 148

def all_channels
  distinct(:channels)
end

.by_device_type(type) ⇒ Parse::Query

Query scope for a specific device type.

Examples:

mac_devices = Parse::Installation.by_device_type(:osx).all

Parameters:

  • type (String, Symbol)

    the device type (ios, android, osx, tvos, watchos, web, expo, win, other, unknown, unsupported)

Returns:



186
187
188
# File 'lib/parse/model/classes/installation.rb', line 186

def by_device_type(type)
  query(device_type: type.to_s)
end

.cleanup_stale_tokens!(days: 90) ⇒ Integer

Delete all installations with stale tokens. Use with caution - this permanently removes installation records.

Examples:

# Clean up installations not updated in 180 days
deleted = Parse::Installation.cleanup_stale_tokens!(days: 180)

Parameters:

  • days (Integer) (defaults to: 90)

    number of days since last update (default: 90)

Returns:

  • (Integer)

    the number of installations deleted



258
259
260
261
262
# File 'lib/parse/model/classes/installation.rb', line 258

def cleanup_stale_tokens!(days: 90)
  installations = stale_tokens(days: days).all
  installations.each(&:destroy)
  installations.count
end

.reset_all_badges(type = :ios) ⇒ Integer

Reset badge count for all installations of a specific device type.

Examples:

Parse::Installation.reset_all_badges
Parse::Installation.reset_all_badges(:android)

Parameters:

  • type (String, Symbol) (defaults to: :ios)

    the device type (default: :ios since badges are primarily iOS)

Returns:

  • (Integer)

    the number of installations updated



214
215
216
217
218
219
220
221
# File 'lib/parse/model/classes/installation.rb', line 214

def reset_all_badges(type = :ios)
  installations = by_device_type(type).where(:badge.gt => 0).all
  installations.each do |installation|
    installation.badge = 0
    installation.save
  end
  installations.count
end

.reset_badges_for_channel(channel) ⇒ Integer

Reset badge count for all installations in a channel.

Examples:

Parse::Installation.reset_badges_for_channel("news")

Parameters:

  • channel (String)

    the channel name

Returns:

  • (Integer)

    the number of installations updated



199
200
201
202
203
204
205
206
# File 'lib/parse/model/classes/installation.rb', line 199

def reset_badges_for_channel(channel)
  installations = subscribers(channel).where(:badge.gt => 0).all
  installations.each do |installation|
    installation.badge = 0
    installation.save
  end
  installations.count
end

.stale_count(days: 90) ⇒ Integer

Count installations with stale tokens.

Examples:

count = Parse::Installation.stale_count(days: 60)

Parameters:

  • days (Integer) (defaults to: 90)

    number of days since last update (default: 90)

Returns:

  • (Integer)

    count of stale installations



247
248
249
# File 'lib/parse/model/classes/installation.rb', line 247

def stale_count(days: 90)
  stale_tokens(days: days).count
end

.stale_tokens(days: 90) ⇒ Parse::Query

Query for installations with stale (old) device tokens. Useful for cleaning up installations that are likely no longer active.

Examples:

# Find installations not updated in 90 days
stale = Parse::Installation.stale_tokens.all

# Find installations not updated in 30 days
stale = Parse::Installation.stale_tokens(days: 30).all

Parameters:

  • days (Integer) (defaults to: 90)

    number of days since last token modification (default: 90)

Returns:

  • (Parse::Query)

    a query for installations with old tokens



237
238
239
240
# File 'lib/parse/model/classes/installation.rb', line 237

def stale_tokens(days: 90)
  cutoff = Time.now - (days * 24 * 60 * 60)
  query(:updated_at.lt => cutoff)
end

.subscribers(channel) ⇒ Parse::Query

Get a query for installations subscribed to a specific channel.

Examples:

# Get all iOS subscribers to the "news" channel
installations = Parse::Installation.subscribers("news")
  .where(device_type: "ios")
  .all

Parameters:

  • channel (String)

    the channel name to find subscribers for

Returns:

  • (Parse::Query)

    a query scoped to the channel’s subscribers



170
171
172
# File 'lib/parse/model/classes/installation.rb', line 170

def subscribers(channel)
  query(:channels.in => [channel])
end

.subscribers_count(channel) ⇒ Integer

Count the number of installations subscribed to a specific channel.

Examples:

count = Parse::Installation.subscribers_count("news")
# => 1250

Parameters:

  • channel (String)

    the channel name to count subscribers for

Returns:

  • (Integer)

    the number of subscribers



158
159
160
# File 'lib/parse/model/classes/installation.rb', line 158

def subscribers_count(channel)
  query(:channels.in => [channel]).count
end

Instance Method Details

#days_since_updateInteger?

Get the number of days since this installation was last updated.

Examples:

puts "Last active #{installation.days_since_update} days ago"

Returns:

  • (Integer, nil)

    days since last update, or nil if no updated_at



351
352
353
354
# File 'lib/parse/model/classes/installation.rb', line 351

def days_since_update
  return nil if updated_at.nil?
  ((Time.now - updated_at.to_time) / (24 * 60 * 60)).to_i
end

#increment_badge!(amount = 1) ⇒ Boolean

Increment the badge count and save.

Examples:

installation.increment_badge!
installation.increment_badge!(5)

Parameters:

  • amount (Integer) (defaults to: 1)

    amount to increment by (default: 1)

Returns:

  • (Boolean)

    true if save was successful



325
326
327
328
# File 'lib/parse/model/classes/installation.rb', line 325

def increment_badge!(amount = 1)
  self.badge = (badge || 0) + amount
  save
end

#reset_badge!Boolean

Reset the badge count to 0 and save.

Examples:

installation.reset_badge!

Returns:

  • (Boolean)

    true if save was successful



314
315
316
317
# File 'lib/parse/model/classes/installation.rb', line 314

def reset_badge!
  self.badge = 0
  save
end

#stale?(days: 90) ⇒ Boolean

Check if this installation’s token is considered stale.

Examples:

if installation.stale?
  puts "This installation may no longer be active"
end

Parameters:

  • days (Integer) (defaults to: 90)

    number of days to consider stale (default: 90)

Returns:

  • (Boolean)

    true if the installation hasn’t been updated in the given days



341
342
343
344
345
# File 'lib/parse/model/classes/installation.rb', line 341

def stale?(days: 90)
  return false if updated_at.nil?
  cutoff = Time.now - (days * 24 * 60 * 60)
  updated_at < cutoff
end

#subscribe(*channel_names) ⇒ Boolean

Subscribe this installation to one or more channels. The changes are automatically saved to the server.

Examples:

installation.subscribe("news", "weather")
installation.subscribe(["sports", "updates"])

Parameters:

  • channel_names (Array<String>)

    the channel names to subscribe to

Returns:

  • (Boolean)

    true if the save was successful



276
277
278
279
280
# File 'lib/parse/model/classes/installation.rb', line 276

def subscribe(*channel_names)
  self.channels ||= []
  self.channels = (self.channels + channel_names.flatten.map(&:to_s)).uniq
  save
end

#subscribed_to?(channel) ⇒ Boolean

Check if this installation is subscribed to a specific channel.

Examples:

if installation.subscribed_to?("news")
  puts "Subscribed to news!"
end

Parameters:

  • channel (String)

    the channel name to check

Returns:

  • (Boolean)

    true if subscribed to the channel



302
303
304
# File 'lib/parse/model/classes/installation.rb', line 302

def subscribed_to?(channel)
  channels&.include?(channel.to_s) || false
end

#unsubscribe(*channel_names) ⇒ Boolean

Unsubscribe this installation from one or more channels. The changes are automatically saved to the server.

Examples:

installation.unsubscribe("news")
installation.unsubscribe("sports", "weather")

Parameters:

  • channel_names (Array<String>)

    the channel names to unsubscribe from

Returns:

  • (Boolean)

    true if the save was successful, or true if no channels were set



289
290
291
292
293
# File 'lib/parse/model/classes/installation.rb', line 289

def unsubscribe(*channel_names)
  return true unless channels.present?
  self.channels = channels - channel_names.flatten.map(&:to_s)
  save
end