Skip to content

Latest commit

 

History

41 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bevy_ios_notifications

Following released Bevy versions crates.io docs.rs discord

Rust crate to easily integrate iOS's native Notification API into a Bevy application.

Talks to UserNotifications directly via objc2 - no Swift package or XCode setup required.

Screenflick.Movie.34.mp4

Demo from our game using this crate: zoolitaire.com

Features

  • change/read badge
  • get remote push deviceToken
  • scheduling local notifications
  • enable/disable presenting notifications while app is foregrounded
  • and the notoriously hard (in unity) to do things like: what notification was clicked

Instructions

  1. Add Rust dependency
  2. Setup Plugin

Use Apple Push Notification Dashboard to simply send push notifications for testing.

1. Add Rust dependency

cargo add bevy_ios_notifications

or

bevy_ios_notifications = { version = "0.8" }

2. Setup Plugin

Initialize Bevy Plugin:

// requests permissions for alerts, sounds and badges
app.add_plugins(bevy_ios_notifications::IosNotificationsPlugin::request_permissions_on_start(true, true, true));

Trigger Alert in your application code:

fn system_triggering_notifications(ios_notifications: IosNotifications) {

    // should be called after the permission response arrives
    ios_notifications.registered_for_push();

    // set app icon badge
    ios_notifications.set_badge(1);

    // schedule a local notification
    let id = ios_notifications.schedule(
        IosNotificationRequest::new()
            .title("title")
            .body("body")
            .trigger(IosNotificationTrigger::one_shot(4))
            // without this the notification is delivered silently
            .sound(IosNotificationSound::Default)
            // if not defined it will be creating a UUID for you
            .identifier("custom id")
            .build(),
    );

}

// this will clear the badge, the notification center and all pending ones
fn process_occluded_events(
    mut e: MessageReader<WindowOccluded>,
    ios_notifications: IosNotifications,
) {
    for ev in e.read() {
        if !ev.occluded {
            ios_notifications.remove_all_pending();
            ios_notifications.remove_all_delivered();
            ios_notifications.set_badge(0);
        }
    }
}

// process async events coming in from ios notification system
fn process_notifications(
    mut events: MessageReader<IosNotificationEvents>,
) {
    for e in events.read() {
        match e {
            IosNotificationEvents::PermissionResponse(_) => todo!(),
            IosNotificationEvents::NotificationSchedulingSucceeded(_) => todo!(),
            IosNotificationEvents::NotificationSchedulingFailed(_) => todo!(),
            IosNotificationEvents::NotificationTriggered(_) => todo!(),
            IosNotificationEvents::PendingNotifications(_) => todo!(),
            IosNotificationEvents::NotificationResponse(_) => todo!(),
            IosNotificationEvents::RemoteNotificationRegistration(_) => todo!(),
        }
    }
}

Implementation Details

  • UNUserNotificationCenter is called directly through objc2, the delegate that receives triggered notifications and their responses is a Rust class (see code)
  • device tokens are only handed to the UIApplicationDelegate, and winit still does not let you hook into its didRegisterForRemoteNotificationsWithDeviceToken callback (see winit PR (dont hold your breath)). So we graft the two remote registration callbacks onto whatever app delegate exists and install our own if there is none (see code)
  • this composes with bevy_ios_app_delegate: if it already installed a delegate we hook that one instead of setting up a second, and an implementation we displace is still called

Our Other Crates

Bevy version support

bevy bevy_ios_notifications
0.19 0.7,0.8,main
0.18 0.6
0.17 0.5
0.16 0.4
0.15 0.3
0.14 0.2
0.13 0.1

License

All code in this repository is dual-licensed under either:

at your option. This means you can select the license you prefer.

Your contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Integrates iOS's native Notification API into a Bevy application

Topics

Resources

Stars

14 stars

Watchers

2 watching

Forks

Sponsor this project

Used by

Contributors

Languages