Device
What a phone lends a Balaur game: local notifications, push tokens, the motor and haptics, the share sheet and the store's review prompt, per platform.
Σε αυτή τη σελίδα
Some services belong to the phone, not the store: a notification, a push token, the motor, the share sheet, the review prompt. They answer the same whichever store runs the game, so a Mac game Steam started still notifies through Apple's notification centre.
- Notifications and push are in the
platformmodule, beside the store's calls. The rest is thedevicemodule. - A call that answers does so through
task::wait, with akind. What arrives unasked isplatform_event. platform::is_availablewithSERVICE_NOTIFICATIONS,SERVICE_PUSH,SERVICE_HAPTICS,SERVICE_SHAREorSERVICE_REVIEWsays whether one works here.
| Call | iOS | macOS | Android | Windows, Linux | A page |
|---|---|---|---|---|---|
platform::notify |
yes | yes | yes | unsupported |
unsupported |
platform::register_for_push |
APNs | APNs | Firebase | unsupported |
unsupported |
device::vibrate, haptic |
yes | nothing | yes | nothing | a short buzz |
device::share |
the share sheet | the sharing picker | the chooser | unsupported |
unsupported |
device::request_review |
the App Store's prompt | the App Store's prompt | Play's, with in-app-review |
unsupported |
unsupported |
Notifications
platform::request_notifications asks the player, and answers
notifications with allowed. On Android 13 and later it is the system's
permission prompt. platform::notify schedules one, and
cancel_notification drops it by its id.
// scripts/reminders.bs
pub class Reminders {
node: Node,
}
impl Reminders {
pub async fn init() {
events::listen(self.node, "platform_event");
let asked = await task::wait(platform::request_notifications());
if !asked["allowed"].as_bool() {
return;
}
platform::notify("Your chest is ready", platform::NotifyOptions {
title: "Chest",
after: 3600.0,
id: "chest",
data: { "chest": 3 },
});
}
pub fn on_platform_event(e: any) {
if e["kind"].as_str() == platform::EVENT_NOTIFICATION_OPENED {
log::info("opened from {e["id"].as_str()}: chest {e["data"]["chest"].as_int()}");
}
}
}
NotifyOptionsholdstitle,after(seconds from now),idanddata. A notification with noidis named after its call.- A tap brings the game forward, and arrives as
notification_openedwith theidanddata. A tap that started the game waits until the first tick listens. - One shown while the game is in front arrives as
notification_received. - On Android the schedule is inexact, and a notification shows the system's reminder icon.
They need no capability on any platform.
Push
platform::register_for_push asks the OS for this device's push token. It
answers push_registered with three fields, spelled as Gamend's
POST /api/v1/me/push_tokens takes them, so the game hands the answer
over as it is.
| Field | iOS, macOS | Android |
|---|---|---|
token |
APNs' device token, hex | Firebase's registration token |
provider |
PUSH_PROVIDER_APNS, "apns" |
PUSH_PROVIDER_FCM, "fcm" |
platform |
PUSH_PLATFORM_IOS, "ios", a Mac's too |
PUSH_PLATFORM_ANDROID, "android" |
// scripts/push.bs
pub class Push {
node: Node,
}
impl Push {
pub async fn init() {
events::listen(self.node, "platform_event");
let token = await task::wait(platform::register_for_push());
if token["kind"].as_str() == platform::EVENT_PUSH_FAILED {
log::warn("no push: {token["error"].as_str()}");
return;
}
if token["kind"].as_str() == platform::EVENT_PUSH_REGISTERED {
// token, provider and platform: POST them to /api/v1/me/push_tokens.
log::info("{token["provider"].as_str()} token for {token["platform"].as_str()}");
}
}
pub fn on_platform_event(e: any) {
let kind = e["kind"].as_str();
if kind == platform::EVENT_PUSH_TOKEN_CHANGED {
// The OS replaced the token: register the new one the same way.
log::info("new token {e["token"].as_str()}");
} else if kind == platform::EVENT_PUSH_RECEIVED {
log::info("push in front: {e["title"].as_str()}");
}
}
}
Arrival on platform_event |
When |
|---|---|
push_token_changed |
the OS replaced the token; the same three fields |
push_received |
a push reached the game in front, with id, title, body and data |
notification_opened |
the player tapped a push, with its data, as for a local one |
- A build without the capability answers
push_failed, with anerrornaming what to add. - A push shows only once the player allowed notifications, so ask with
request_notificationsfirst. - Apple needs
[apple] capabilities = ["push-notifications"]: see Apple services. - Android needs
[android] capabilities = ["firebase-messaging"]andgoogle_services: see Google services.
Haptics and the motor
device::vibrate(seconds)buzzes the motor: Android's vibrator, a page'snavigator.vibrate, or the iPhone's one system buzz whatever the length. It replacesinput::vibrate.device::haptictaps once, named by a constant:HAPTIC_LIGHT,HAPTIC_MEDIUM,HAPTIC_HEAVY,HAPTIC_SELECTION,HAPTIC_SUCCESS,HAPTIC_WARNINGorHAPTIC_ERROR. iOS's feedback generators, Android's predefined effects, a short buzz on a page.- Both do nothing on a desktop. They are output, so a recording keeps neither and a replay runs neither, as with a gamepad's rumble.
Sharing and the review prompt
// scripts/juice.bs
pub class Juice {
node: Node,
}
impl Juice {
pub fn on_hit() {
device::haptic(device::HAPTIC_HEAVY);
device::vibrate(0.2);
}
pub async fn share_score(score: int) {
let shared = await task::wait(device::share(device::ShareOptions {
text: "I scored {score} in Dragon Run",
title: "Share your score",
}));
if shared["kind"].as_str() == device::EVENT_SHARED {
log::info("shared to {shared["target"].as_str()}");
}
}
pub async fn level_cleared(level: int) {
if level == 5 && platform::is_available(platform::SERVICE_REVIEW) {
await task::wait(device::request_review());
}
}
}
device::sharetakestext, afile, or both, and the sheet'stitle. A file is project-relative, or in the user data directory.- It answers
sharedwith thetargetthe player picked where the OS names it,cancelled,error, orunsupported. device::request_reviewasks the store to show its prompt to rate the game, and answersdone. The store decides whether it shows, and says nothing about what the player did.- On Android the review prompt needs
[android] capabilities = ["in-app-review"], and shows only in a build installed from Play.
Not yet run on a device
Every call here is tested headless, through a canned OS, stand-ins and a replay, and the Java behind Android's compiles and dexes. None has yet run on a phone: no notification tapped, no file shared, no haptic felt, and no push sent through APNs or Firebase. The device module's reference lists every call and constant.