Skip to content

Commit e2f1de0

Browse files
feat: add flag to use direct apns or fcm for iOS notifications
1 parent 8cebff7 commit e2f1de0

12 files changed

Lines changed: 126 additions & 48 deletions

src/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::{
3737
apns::APNSSender,
3838
fcm::FcmSender,
3939
metrics::{metrics_handler, HttpMetricsMiddleware},
40-
models::{DataMessageType, Metrics, Notification, PushGatewayResponse},
40+
models::{Metrics, Notification, PushGatewayResponse},
4141
pusher,
4242
settings::Settings,
4343
};
@@ -64,8 +64,8 @@ pub async fn matrix_push(
6464
let mut retry_time = Duration::from_millis(250);
6565
let mut attempt = 0;
6666
loop {
67-
if let Err(e) = match dev.data_message_type() {
68-
DataMessageType::Ios => {
67+
if let Err(e) = match dev.use_direct_apns {
68+
Some(true) => {
6969
pusher::push_notification_apns(&notification, dev, &apns_sender, &settings)
7070
.await
7171
}

src/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ pub struct Device {
7070
/// A dictionary of customisations made to the way this notification is to
7171
/// be presented.
7272
pub tweaks: Option<serde_json::Value>,
73+
/// Whether to use fcm or apns for iOS notifications
74+
pub use_direct_apns: Option<bool>,
7375
}
7476

7577
/// What kind of data message should be sent (if any)

src/pusher.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,36 @@ pub async fn push_notification_fcm(
111111
body.android(android_config);
112112
body.apns(ios_config);
113113
}
114-
// we should never end up here as this case is handled by push_notification_apns
115-
DataMessageType::Ios => {}
114+
DataMessageType::Ios => {
115+
// Used for background notification handling on iOS, if enabled by the app
116+
117+
// If there's no room_id then this is a badge only notification that must not
118+
// have any notification content
119+
if notification.room_id.is_some() {
120+
// If apple decide not to run the service extension there needs to be a fallback
121+
// notification
122+
body.notification(fcm_notification);
123+
}
124+
125+
body.data(notification.data(device)?)?;
126+
127+
let mut ios_config = ApnsConfig::new();
128+
ios_config.payload(json!({
129+
"aps": {
130+
"mutable-content": 1,
131+
"badge": count,
132+
"sound": settings.hedwig.notification_sound
133+
}
134+
}))?;
135+
136+
// Priority needs to be 5 for the service extension to be used
137+
ios_config.headers(ApnsHeaders {
138+
apns_priority: "5".to_owned(),
139+
apns_push_type: settings.hedwig.apns_push_type.0.to_string(),
140+
})?;
141+
142+
body.apns(ios_config);
143+
}
116144
};
117145

118146
sender.lock().await.send(body).await?;

tests/message_android.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"token":"Android","data":{"content":"null","counts":"{\"unread\":1337,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊\",\"pushkey\":\"Android\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":\"android\",\"format\":\"event_id_only\"},\"tweaks\":null}]","prio":"\"high\"","room_id":"owo"},"android":{"priority":"high","direct_boot_ok":false}}
1+
{"token":"Android","data":{"content":"null","counts":"{\"unread\":1337,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊\",\"pushkey\":\"Android\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":\"android\",\"format\":\"event_id_only\"},\"tweaks\":null,\"use_direct_apns\":null}]","prio":"\"high\"","room_id":"owo"},"android":{"priority":"high","direct_boot_ok":false}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"token":"Android","data":{"content":"null","counts":"{\"unread\":0,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊\",\"pushkey\":\"Android\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":\"android\",\"format\":\"event_id_only\"},\"tweaks\":null}]","prio":"\"high\""},"android":{"priority":"high","direct_boot_ok":false}}
1+
{"token":"Android","data":{"content":"null","counts":"{\"unread\":0,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊\",\"pushkey\":\"Android\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":\"android\",\"format\":\"event_id_only\"},\"tweaks\":null,\"use_direct_apns\":null}]","prio":"\"high\""},"android":{"priority":"high","direct_boot_ok":false}}

tests/message_android_legacy.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"token":"AndroidLegacy","data":{"content":"null","counts":"{\"unread\":1337,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊.data_message\",\"pushkey\":\"AndroidLegacy\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":null,\"format\":\"event_id_only\"},\"tweaks\":null}]","prio":"\"high\"","room_id":"owo"},"android":{"priority":"high","direct_boot_ok":false}}
1+
{"token":"AndroidLegacy","data":{"content":"null","counts":"{\"unread\":1337,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊.data_message\",\"pushkey\":\"AndroidLegacy\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":null,\"format\":\"event_id_only\"},\"tweaks\":null,\"use_direct_apns\":null}]","prio":"\"high\"","room_id":"owo"},"android":{"priority":"high","direct_boot_ok":false}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"token":"AndroidLegacy","data":{"content":"null","counts":"{\"unread\":0,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊.data_message\",\"pushkey\":\"AndroidLegacy\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":null,\"format\":\"event_id_only\"},\"tweaks\":null}]","prio":"\"high\""},"android":{"priority":"high","direct_boot_ok":false}}
1+
{"token":"AndroidLegacy","data":{"content":"null","counts":"{\"unread\":0,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊.data_message\",\"pushkey\":\"AndroidLegacy\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":null,\"format\":\"event_id_only\"},\"tweaks\":null,\"use_direct_apns\":null}]","prio":"\"high\""},"android":{"priority":"high","direct_boot_ok":false}}
File renamed without changes.

tests/message_ios_fcm.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"token":"IoS","data":{"content":"null","counts":"{\"unread\":1337,\"missed_calls\":null}","devices":"[{\"app_id\":\"com.famedly.🦊\",\"pushkey\":\"IoS\",\"pushkey_ts\":1655896032,\"data\":{\"data_message\":\"ios\",\"format\":\"event_id_only\"},\"tweaks\":null,\"use_direct_apns\":false}]","prio":"\"high\"","room_id":"owo"},"notification":{"title":"🦊 1337 🦊","body":"read the notification pls :c"},"apns":{"headers":{"apns-priority":"5","apns-push-type":"background"},"payload":{"aps":{"badge":1337,"mutable-content":1,"sound":"default"}}}}

0 commit comments

Comments
 (0)