cengiz-pz / godot-android-notification-scheduler-plugin

Notification Scheduler Plugin allows scheduling of local notifications on the Android platform.
MIT License
23 stars 4 forks source link

"GodotAndroidNotificationSchedulerPlugin singleton not initialized!" When trying to use plugin #2

Closed wychwitch closed 7 months ago

wychwitch commented 7 months ago

I'm sorry if I missed something obvious, but I've been slamming my head against the wall trying to figure this out. I followed the instructions to set up the plugin, including installing to the addons folder (which I created) and created a button that should trigger the notification when pressed. However it does nothing and using the adb logcat I saw the error GodotAndroidNotificationSchedulerPlugin singleton not initialized! pop up.

Everything else seems to work, including the plugin managing to request permissions, but it just fails when trying to schedule the notification.

Also just to be sure, I copied the drawable icon over from android studio into the android/build/res folder (it was listed as drawable, and its type is .xml)

Here's my code, which is just placed on the root 2d node.

extends Node2D

var label: Label;
var pressed = 0;
# Called when the node enters the scene tree for the first time.
func _ready():
    label = get_node("Label")
    if !$NotificationScheduler.has_post_notifications_permission():
        $NotificationScheduler.request_post_notifications_permission()
    $NotificationScheduler.create_notification_channel("my_channel_id", "My Channel Name", "My channel description")

func _on_button_pressed():
    label.text = "Pressed! " + str(pressed);
    pressed += 1;
    var my_notification_data = NotificationData.new()
    my_notification_data.set_id(9).\
            set_channel_id("my_channel_id").\
            set_title("My Notification Title").\
            set_content("My notification content").\
            set_small_icon_name("ic_notification")

    $NotificationScheduler.schedule(
            my_notification_data,
            5
        )

I'm using android 14 if that matters! Any help would be amazing. Thank you for making such a great plugin!!

cengiz-pz commented 7 months ago

Hello @wychwitch,

I think that I have found the problem. The problem is that I have forgotten to add an important step to the documentation.

Apologies :(

Please add the following line to your code:

func _ready():
    $NotificationScheduler.init()  # please add this line to initialize the plugin node
    # then you can start using the node methods
    if !$NotificationScheduler.has_post_notifications_permission():
        ...

Otherwise, your code looks perfect.

Thanks for using the plugin.

wychwitch commented 7 months ago

Yep that was it, thank you!

Unfortunately I now have two other issues, the first is that I have the same crashing problem as issue #1 , which I'll post in there in a bit hahaha

The second issue is that the notification isn't sent while the game is in the background. Is that intended behavior? I was hoping for it to be scheduled to go off after a certain amount of time regardless of if the game is open or not.

Either way, thank you so much for this plugin! I tried for weeks to make one myself but I never was able to get something working

edit: I think what I was describing is actually something that uses the permission <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> (found here), though I'm sure if you've already seen this!

cengiz-pz commented 7 months ago

Hi @wychwitch, I believe that both of your issues are a result of the bug mentioned in #1 . The alarms should be triggered whether your app is running or not. This should be working on devices running Android 11.