Skip to content

Commit

Permalink
Route sandbox: tokens to sandbox endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Feb 29, 2024
1 parent ee76849 commit 9a8f68d
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use a2::{
Client, DefaultNotificationBuilder, Endpoint, Error::ResponseError, NotificationBuilder,
NotificationOptions, Priority,
};
use anyhow::Result;
use anyhow::{Context as _, Result};
use async_std::prelude::*;
use log::*;
use std::io::Seek;

pub async fn start(
db: &sled::Db,
Expand All @@ -17,22 +18,30 @@ pub async fn start(
"Waking up devices every {}",
humantime::format_duration(interval)
);
let endpoint = Endpoint::Production;
let client = Client::certificate(&mut certificate, password, endpoint)?;
let production_client = Client::certificate(&mut certificate, password, Endpoint::Production)
.context("Failed to create production client")?;
certificate.rewind()?;
let sandbox_client = Client::certificate(&mut certificate, password, Endpoint::Sandbox)
.context("Failed to create sandbox client")?;

// first wakeup on startup
wakeup(db, &client, topic).await;
wakeup(db, &production_client, &sandbox_client, topic).await;

// create interval
let mut interval = async_std::stream::interval(interval);
while interval.next().await.is_some() {
wakeup(db, &client, topic).await;
wakeup(db, &production_client, &sandbox_client, topic).await;
}

Ok(())
}

async fn wakeup(db: &sled::Db, client: &Client, topic: Option<&str>) {
async fn wakeup(
db: &sled::Db,
production_client: &Client,
sandbox_client: &Client,
topic: Option<&str>,
) {
let tokens = db
.iter()
.filter_map(|entry| match entry {
Expand All @@ -46,14 +55,21 @@ async fn wakeup(db: &sled::Db, client: &Client, topic: Option<&str>) {
for device_token in tokens {
info!("notify: {}", device_token);

let (client, device_token) =
if let Some(sandbox_token) = device_token.strip_prefix("sandbox:") {
(sandbox_client, sandbox_token)
} else {
(production_client, device_token.as_str())
};

// Sent silent notification.
// According to <https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification>
// to send a silent notification you need to set background notification flag `content-available` to 1
// and don't include `alert`, `badge` or `sound`.
let payload = DefaultNotificationBuilder::new()
.set_content_available()
.build(
&device_token,
device_token,
NotificationOptions {
// Normal priority (5) means
// "send the notification based on power considerations on the user’s device".
Expand All @@ -75,7 +91,7 @@ async fn wakeup(db: &sled::Db, client: &Client, topic: Option<&str>) {
},
Err(ResponseError(res)) => {
info!("Removing token {} due to error {:?}.", &device_token, res);
if let Err(err) = db.remove(&device_token) {
if let Err(err) = db.remove(device_token) {
error!("failed to remove {}: {:?}", &device_token, err);
}
}
Expand Down

0 comments on commit 9a8f68d

Please sign in to comment.