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:

/pushhub-sw.js
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).

html
<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
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
// 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());
python
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
<?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);
response (200)
{
  "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

IzinaIbisobanuro
titleUmutwe w'amenyesha (ngombwa).
bodyUbutumwa (ngombwa).
urlURL ifungurwa iyo amenyesha akozweho.
iconURL y'ishusho nto.
imageURL y'ishusho nini (Android).
externalIdsUrutonde rwa externalId zo kohererezwa.
tagsUrutonde rwa tags; uwo ahura n'imwe arabona.
ttlIgihe (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/subscribeKwandika subscription ya browser (SDK ibikoresha).
POST/api/v1/unsubscribeGukuraho subscription.
POST/api/v1/send4. Ohereza amenyesha ukoresheje REST

Amakosa asanzwe

401invalid_secret_key / invalid_public_key
400title_and_body_required / invalid_subscription
413payload_too_large
405method_not_allowed
500vapid_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.