Module: ProMotion::DelegateNotifications

Defined in:
lib/ProMotion/delegate_notifications.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aps_notificationObject

Returns the value of attribute aps_notification.



5
6
7
# File 'lib/ProMotion/delegate_notifications.rb', line 5

def aps_notification
  @aps_notification
end

Instance Method Details

#actionable_notifications?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ProMotion/delegate_notifications.rb', line 48

def actionable_notifications?
  UIApplication.sharedApplication.respond_to?(:registerUserNotificationSettings)
end

#application(application, handleActionWithIdentifier: action_identifier, forRemoteNotification: notification, completionHandler: callback) ⇒ Object

CocoaTouch



84
85
86
# File 'lib/ProMotion/delegate_notifications.rb', line 84

def application(application, didRegisterForRemoteNotificationsWithDeviceToken: device_token)
  on_push_registration(device_token, nil) if respond_to?(:on_push_registration)
end

#check_for_push_notification(options) ⇒ Object



7
8
9
10
11
# File 'lib/ProMotion/delegate_notifications.rb', line 7

def check_for_push_notification(options)
  if options && options[UIApplicationLaunchOptionsRemoteNotificationKey]
    received_push_notification options[UIApplicationLaunchOptionsRemoteNotificationKey], true
  end
end

#received_push_notification(notification, was_launched) ⇒ Object



77
78
79
80
# File 'lib/ProMotion/delegate_notifications.rb', line 77

def received_push_notification(notification, was_launched)
  @aps_notification = ProMotion::PushNotification.new(notification)
  on_push_notification(@aps_notification, was_launched) if respond_to?(:on_push_notification)
end

#received_push_notification_with_action(notification, action) ⇒ Object



72
73
74
75
# File 'lib/ProMotion/delegate_notifications.rb', line 72

def received_push_notification_with_action(notification, action)
  @aps_notification = ProMotion::PushNotification.new(notification)
  on_push_notification_action(action, @aps_notification) if respond_to?(:on_push_notification_action)
end

#register_for_push_notification_types(types) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ProMotion/delegate_notifications.rb', line 36

def register_for_push_notification_types(types)
  UIApplication.sharedApplication.tap do |application|
    if actionable_notifications?
      settings = UIUserNotificationSettings.settingsForTypes(types, categories: @push_notification_categories)
      application.registerUserNotificationSettings settings
      application.registerForRemoteNotifications
    else
      application.registerForRemoteNotificationTypes types
    end
  end
end

#register_for_push_notifications(*notification_types) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ProMotion/delegate_notifications.rb', line 26

def register_for_push_notifications(*notification_types)
  notification_types = Array.new(notification_types)
  notification_types = [ :badge, :sound, :alert, :newsstand ] if notification_types.include?(:all)

  types = UIRemoteNotificationTypeNone
  notification_types.each { |t| types = types | map_notification_symbol(t) }

  register_for_push_notification_types(types)
end

#register_push_notification_category(category_name, actions, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ProMotion/delegate_notifications.rb', line 13

def register_push_notification_category(category_name, actions, options = {})
  return unless actionable_notifications?

  @push_notification_categories ||= []
  UIMutableUserNotificationCategory.new.tap do |category|
    minimal = options[:minimal]
    category.setActions(minimal, forContext: UIUserNotificationActionContextMinimal) if minimal
    category.setActions(actions, forContext: UIUserNotificationActionContextDefault)
    category.identifier = category_name
    @push_notification_categories << category
  end
end

#registered_push_notificationsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ProMotion/delegate_notifications.rb', line 56

def registered_push_notifications
  types = []
  if UIApplication.sharedApplication.respond_to?(:currentUserNotificationSettings)
    mask = UIApplication.sharedApplication.currentUserNotificationSettings.types
  else
    mask = UIApplication.sharedApplication.enabledRemoteNotificationTypes
  end

  types << :badge     if mask & UIRemoteNotificationTypeBadge > 0
  types << :sound     if mask & UIRemoteNotificationTypeSound > 0
  types << :alert     if mask & UIRemoteNotificationTypeAlert > 0
  types << :newsstand if mask & UIRemoteNotificationTypeNewsstandContentAvailability > 0

  types
end

#unregister_for_push_notificationsObject



52
53
54
# File 'lib/ProMotion/delegate_notifications.rb', line 52

def unregister_for_push_notifications
  UIApplication.sharedApplication.unregisterForRemoteNotifications
end