Ibisobanuro bya API
Aha niho wiga gukoresha PushHub mu mushinga wawe: uhereye ku kubona API keys kugeza ku kohereza amenyesha.
βοΈ Ibisobanuro bya Email API β π¬ Chat β
1. Kubona API keys
Iyandikishe, winjire, ukore umushinga mu Mbonerahamwe. Uzabona urufunguzo rusange (pk_...) n'urufunguzo rw'ibanga (sk_...).
2. Shyira service worker ku rubuga rwawe
Kora dosiye /pushhub-sw.js mu mizi y'urubuga rwawe (ikeneye kuba kuri domaine yawe, kandi ku HTTPS) ushyiremo uyu murongo umwe:
importScripts('https://push.newtalentsg.co.rw/pushhub-sw.js');3. Shyiramo SDK uyobore subscribe
Shyira script ku paji yawe, uhamagare init, hanyuma subscribe ubwo umukoresha akanze buto (browser isaba uruhushya iyo ikanzwe).
<script src="https://push.newtalentsg.co.rw/sdk.js"></script>
<button id="enable-push">π Enable notifications</button>
<script>
PushHub.init({
publicKey: 'pk_xxxxxxxxxxxxxxxx', // your public key
// swPath: '/pushhub-sw.js' // optional (default)
}).then(function () {
document.getElementById('enable-push').onclick = function () {
PushHub.subscribe({
externalId: 'user-123', // optional: your own user ID
tags: ['news', 'kigali'] // optional
})
.then(function () { alert('Subscribed!'); })
.catch(function (err) { console.error(err.message); });
};
});
</script>4. Ohereza amenyesha ukoresheje REST
Ohereza POST kuri /api/v1/send ukoresheje urufunguzo rw'ibanga mu mutwe wa Authorization. Ibi bikorwe kuri seriveri yawe gusa.
curl -X POST https://push.newtalentsg.co.rw/api/v1/send \
-H "Authorization: Bearer sk_xxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{
"title": "Muraho!",
"body": "Ubu ni ubutumwa bwa mbere.",
"url": "https://example.com/news/1",
"icon": "https://example.com/icon-192.png"
}'// Node.js 18+ (server only)
const res = await fetch('https://push.newtalentsg.co.rw/api/v1/send', {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + process.env.PUSHHUB_SECRET_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: 'Hello',
body: 'Notification for selected users',
externalIds: ['user-123', 'user-456'], // or tags: ['news']
}),
});
console.log(await res.json());import os, requests
r = requests.post(
"https://push.newtalentsg.co.rw/api/v1/send",
headers={"Authorization": "Bearer " + os.environ["PUSHHUB_SECRET_KEY"]},
json={"title": "Hello", "body": "From Python", "tags": ["news"]},
)
print(r.json())<?php
$ch = curl_init('https://push.newtalentsg.co.rw/api/v1/send');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('PUSHHUB_SECRET_KEY'),
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode(['title' => 'Hello', 'body' => 'From PHP']),
]);
echo curl_exec($ch);{
"success": true,
"id": "kP3x...",
"recipients": 42,
"sent": 40,
"failed": 2,
"removed": 2
}5. Intego (targeting)
Muri subscribe ushobora gushyiramo externalId (urugero ID y'umukoresha wawe) na tags. Iyo wohereza, ushobora kugena externalIds cyangwa tags kugira ngo ubigeze ku bantu bamwe gusa.
Parameters za /api/v1/send
| Izina | Ibisobanuro |
|---|---|
title | Umutwe w'amenyesha (ngombwa). |
body | Ubutumwa (ngombwa). |
url | URL ifungurwa iyo amenyesha akozweho. |
icon | URL y'ishusho nto. |
image | URL y'ishusho nini (Android). |
externalIds | Urutonde rwa externalId zo kohererezwa. |
tags | Urutonde rwa tags; uwo ahura n'imwe arabona. |
ttl | Igihe (amasegonda) amenyesha amara ategereje igihe telefoni ifunze. Ibisanzwe: 86400. |
Izindi endpoints
| GET | /api/v1/config?key=pk_... | Gusoma VAPID public key n'izina ry'umushinga (SDK ibikoresha). |
| POST | /api/v1/subscribe | Kwandika subscription ya browser (SDK ibikoresha). |
| POST | /api/v1/unsubscribe | Gukuraho subscription. |
| POST | /api/v1/send | 4. Ohereza amenyesha ukoresheje REST |
Amakosa asanzwe
401 | invalid_secret_key / invalid_public_key |
400 | title_and_body_required / invalid_subscription |
413 | payload_too_large |
405 | method_not_allowed |
500 | vapid_not_configured / server_error |
Iby'ingenzi
- Amenyesha akora gusa ku rubuga rwa HTTPS (localhost nabyo biremewe mu iterambere).
- Kuri Android koresha Chrome (cyangwa Firefox, Edge, Samsung Internet).
- Ubunini bw'ubutumwa (payload) ntibugomba kurenga 4KB.
- Ntuzigere ushyira urufunguzo rw'ibanga (sk_) muri code ya frontend.