require'rest-client'REST_API_HEADERS={'Content-Type'=>'application/json'}.freeze# TODO: 1. Replace with your APP ID and REST API Key# Example: b2f7f966-d8cc-11e4-bed1-df8f05be55baONESIGNAL_APP_ID='<YOUR_ONESIGNAL_APP_ID>'.freeze# Example: NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJjONESIGNAL_REST_API_KEY='<YOUR_ONESIGNAL_APP_REST_API_KEY>'.freeze# TODO: 2. Query your database and generate an array of hashes; structured like this:email_player_id_list=[{email: 'email_1@domain.com',device_player_id: '3b3e6417-b8fb-41b4-9b2b-552ee4818f11'},{email: 'email_2@domain.com',device_player_id: '3b3e6417-b8fb-41b4-9b2b-552ee4818f12'}]email_player_id_list.eachdo |email_with_device|
response=create_email(email_with_device)# TODO: (OPTIONAL):# 3. Save response[:id] as the player_id for the newly created email into your databaseifresponse[:success]endenddefcreate_email(email_with_device)device=email_with_deviceemail_auth_hash=OpenSSL::HMAC.hexdigest('sha256',ONESIGNAL_REST_API_KEY,device[:email])email_create_params={device_type: 11,app_id: ONESIGNAL_APP_ID,identifier: device[:email],device_player_id: device[:device_player_id],email_auth_hash: email_auth_hash}email_player_id=nilbegin# Create email recordresponse=RestClient::Request.execute(method: :post,url: 'https://onesignal.com/api/v1/players',headers: REST_API_HEADERS,payload: email_create_params.to_json)email_player_id=JSON.parse(response.body)['id']device_update_params={app_id: ONESIGNAL_APP_ID,email: device[:email],parent_player_id: email_player_id}# Link email to deviceRestClient::Request.execute(method: :put,url: "https://onesignal.com/api/v1/players/#{device[:device_player_id]}",headers: REST_API_HEADERS,payload: device_update_params.to_json)rescue=>eputs"Error creating email record for: #{device[:email]}"putsereturn{success: false,id: email_player_id}end{success: true,id: email_player_id}end
Import Emails without Player Id
Ruby
require'rest-client'REST_API_HEADERS={'Content-Type'=>'application/json'}.freeze# TODO: 1. Replace with your APP ID and REST API Key# Example: b2f7f966-d8cc-11e4-bed1-df8f05be55baONESIGNAL_APP_ID='<YOUR_ONESIGNAL_APP_ID>'.freeze# Example: NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJjONESIGNAL_REST_API_KEY='<YOUR_ONESIGNAL_APP_REST_API_KEY>'.freeze# TODO: 2. Query your database and generate an array of hashes; structured like this:email_list=['email_1@domain.com','email_2@domain.com']email_list.eachdo |email|
response=create_email(email)# TODO: (OPTIONAL):# 3. Save response[:id] as the player_id for the newly created email into your databaseifresponse[:success]endenddefcreate_email(email)email_auth_hash=OpenSSL::HMAC.hexdigest('sha256',ONESIGNAL_REST_API_KEY,email)email_create_params={device_type: 11,app_id: ONESIGNAL_APP_ID,identifier: email,email_auth_hash: email_auth_hash}email_player_id=nilbegin# Create email recordresponse=RestClient::Request.execute(method: :post,url: 'https://onesignal.com/api/v1/players',headers: REST_API_HEADERS,payload: email_create_params.to_json)email_player_id=JSON.parse(response.body)['id']rescue=>eputs"Error creating email record for: #{email}"putsereturn{success: false,id: email_player_id}end{success: true,id: email_player_id}end