Last active
August 15, 2019 01:18
-
-
Save num8er/f6292112e6ba0f44b68a3fa93bd9a072 to your computer and use it in GitHub Desktop.
Sending push notification using OneSignal API where title and content passed from iOS app as json body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func panic() { | |
let url = URL(string: "https://mywebsite.com/send.php")! | |
let parameters = ["title": "Some title", "content": "Some content"] | |
let body = try! JSONSerialization.data(withJSONObject: parameters, options: []) | |
let request = URLRequest(url: url) | |
request.httpMethod = "POST" | |
request.setValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.httpBody = body | |
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in | |
if let error = error { | |
print("error:", error) | |
return | |
} | |
do { | |
guard let data = data else { return } | |
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject] else { return } | |
print("json:", json) | |
} catch { | |
print("error:", error) | |
} | |
} | |
task.resume() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sendMessage($input) { | |
$content = ["en" => $input['content']]; | |
$title = ["en" => $input['title']]; | |
$body = json_encode([ | |
'app_id' => "MYAPPID", | |
'included_segments' => ['All'] | |
'data' => ["foo" => "bar"], | |
'contents' => $content, | |
'headings' => $title | |
]); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications"); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
'Content-Type: application/json; charset=utf-8', | |
'Authorization: Basic MYAPIKEY' | |
]); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
$input = json_decode(file_get_contents('php://input'), true); | |
$response = sendMessage($input); | |
header('Content-Type: application/json'); | |
print $response; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment