#bevy-plugin #ios #push-notifications #bevy #mobile #swift #gamedev

bevy_ios_notifications

Bevy plugin to interact with iOS Notifications API

11 releases

new 0.3.0 Dec 2, 2024
0.2.3 Nov 26, 2024
0.2.0 Jul 11, 2024
0.1.6 Apr 25, 2024

#1375 in Game dev

Download history 14/week @ 2024-08-25 61/week @ 2024-09-01 167/week @ 2024-09-08 46/week @ 2024-09-15 25/week @ 2024-09-22 51/week @ 2024-09-29 64/week @ 2024-10-06 45/week @ 2024-10-13 17/week @ 2024-10-20 183/week @ 2024-10-27 88/week @ 2024-11-03 63/week @ 2024-11-10 55/week @ 2024-11-17 177/week @ 2024-11-24 172/week @ 2024-12-01

471 downloads per month

MIT license

24KB
449 lines

bevy_ios_notifications

Following released Bevy versions crates.io docs.rs discord

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

https://github.com/rustunit/bevy_ios_notifications/assets/776816/78e9d708-1cdd-4e54-af2a-c6a5b787f98b

Demo from our game using this crate: zoolitaire.com

See also bevy_ios_iap, bevy_ios_alerts, bevy_ios_gamecenter, bevy_ios_review & bevy_ios_impact

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 to XCode: Add SPM (Swift Package Manager) dependency
  2. Add Rust dependency
  3. Setup Plugin

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

1. Add to XCode

Go to File -> Add Package Dependencies and paste https://github.com/rustunit/bevy_ios_notifications.git into the search bar on the top right: xcode

2. Add Rust dependency

cargo add bevy_ios_notifications

or

bevy_ios_notifications = { version = "0.2" }

3. 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: NonSend<IosNotificationsResource>) {

    // 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 = IosNotificationsResource::schedule(
        IosNotificationRequest::new()
            .title("title")
            .body("body")
            .trigger(IosNotificationTrigger::one_shot(4))
            // 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: EventReader<WindowOccluded>,
    ios_notifications: NonSend<IosNotificationsResource>,
) {
    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: EventReader<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

  • due to the more complex nature of the types that need to be send in and out this crates uses protobuf for that (see schema)
  • because winit currently does not let you hook into the AppDelegates's didRegisterForRemoteNotificationsWithDeviceToken callback we use method swizzling to intercept these (see code, see winit PR (dont hold your breath))

Bevy version support

bevy bevy_ios_notifications
0.15 0.3,main
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.

Dependencies

~22–33MB
~542K SLoC