|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Fcm |
| 4 | + class Client |
| 5 | + # A Fcm Client class to handle notification setting methods |
| 6 | + module NotificationSetting |
| 7 | + GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com' |
| 8 | + END_POINT = '/gcm/notification' |
| 9 | + |
| 10 | + def create_notification_key(key_name, project_id, registration_ids = []) |
| 11 | + post_body = build_post_body( |
| 12 | + registration_ids, |
| 13 | + operation: 'create', |
| 14 | + notification_key_name: key_name |
| 15 | + ) |
| 16 | + res = make_request( |
| 17 | + :post, |
| 18 | + GROUP_NOTIFICATION_BASE_URI, |
| 19 | + END_POINT, |
| 20 | + post_body, |
| 21 | + authorization_headers.merge('project_id' => project_id) |
| 22 | + ) |
| 23 | + Fcm::Response.build_fcm_response(res) |
| 24 | + end |
| 25 | + |
| 26 | + def add_registration_ids(key_name, project_id, notification_key, register_ids) |
| 27 | + post_body = build_post_body( |
| 28 | + register_ids, |
| 29 | + operation: 'add', |
| 30 | + notification_key_name: key_name, |
| 31 | + notification_key: notification_key |
| 32 | + ) |
| 33 | + res = make_request( |
| 34 | + :post, |
| 35 | + GROUP_NOTIFICATION_BASE_URI, |
| 36 | + END_POINT, |
| 37 | + post_body, |
| 38 | + authorization_headers.merge('project_id' => project_id) |
| 39 | + ) |
| 40 | + Fcm::Response.build_fcm_response(res) |
| 41 | + end |
| 42 | + |
| 43 | + def remove_registration_ids(key_name, project_id, notif_key, register_ids) |
| 44 | + post_body = build_post_body( |
| 45 | + register_ids, |
| 46 | + operation: 'remove', |
| 47 | + notification_key_name: key_name, |
| 48 | + notification_key: notif_key |
| 49 | + ) |
| 50 | + res = make_request( |
| 51 | + :post, |
| 52 | + GROUP_NOTIFICATION_BASE_URI, |
| 53 | + END_POINT, |
| 54 | + post_body, |
| 55 | + authorization_headers.merge('project_id' => project_id) |
| 56 | + ) |
| 57 | + Fcm::Response.build_fcm_response(res) |
| 58 | + end |
| 59 | + |
| 60 | + def recover_notification_key(key_name, project_id) |
| 61 | + params = { notification_key_name: key_name } |
| 62 | + res = make_request( |
| 63 | + :get, |
| 64 | + GROUP_NOTIFICATION_BASE_URI, |
| 65 | + END_POINT, |
| 66 | + params, |
| 67 | + authorization_headers.merge('project_id' => project_id) |
| 68 | + ) |
| 69 | + Fcm::Response.build_fcm_response(res) |
| 70 | + end |
| 71 | + |
| 72 | + private |
| 73 | + |
| 74 | + def build_post_body(registration_ids, options = {}) |
| 75 | + ids = if registration_ids.is_a?(String) |
| 76 | + [registration_ids] |
| 77 | + else |
| 78 | + registration_ids |
| 79 | + end |
| 80 | + { registration_ids: ids }.merge(options) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments