OneSignal / OneSignal-Flutter-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your flutter app with OneSignal
https://www.onesignal.com
Other
625 stars 215 forks source link

[Bug]: Unwanted dialog after allow notification permission #784

Closed tommylung closed 11 months ago

tommylung commented 1 year ago

What happened?

In Flutter SDK v5.0.3, here is the prompt for notifications when I first allow the notification permission.

Steps to reproduce?

1. Install SDK v5.0.3
2. Init OneSignal
3. Build app
4. Allow notification permission
5. Alert prompt

What did you expect to happen?

It is expected that no alert will be prompted. You currently have notifications truned off for this application. You can open Settings to re-enable them

Screenshot 2023-11-03 at 6 03 04 PM

Moreover, the devices cannot receive the push notification after upgrading to SDK v5.0.3. I can receive notifications from the previous SDK version.

OneSignal Flutter SDK version

5.0.3

Which platform(s) are affected?

Relevant log output

No response

Code of Conduct

emawby commented 1 year ago

@tommylung Hello thank you for reaching out. That prompt is typically shown when the app has requested permission after the permissions have already been rejected. Could you share your code setting up OneSignal? Is this always happening even for fresh installs (uninstall/reinstall) or is it only happening for overwritten builds or hot reloads?

tommylung commented 1 year ago

@emawby Thanks for your reply.

Yes, this prompt always happens for fresh installs and cannot receive any notification from OneSignal.

Here is the code for setting up OneSignal. I implemented the code in a screen that after the main.dart.

static void setOneSignal() {
  //Remove this method to stop OneSignal Debugging
  OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
  OneSignal.initialize(Constants.onesignalId);
  OneSignal.Notifications.requestPermission(true);

  OneSignal.User.pushSubscription.addObserver((state) {
    debugPrint(state.current.jsonRepresentation());
    debugPrint("[OneSignal] User ID: ${state.current.id}");
  });

  OneSignal.Notifications.addPermissionObserver((state) {
    debugPrint("[OneSignal] Has permission $state");
  });

  OneSignal.Notifications.addClickListener((event) {
    debugPrint(
        '[OneSignal] NOTIFICATION CLICK LISTENER CALLED WITH EVENT: $event');
  });

  OneSignal.Notifications.addForegroundWillDisplayListener((event) {
    /// preventDefault to not display the notification
    event.preventDefault();

    /// notification.display() to display after preventing default
    event.notification.display();
  });
}
tommylung commented 11 months ago

I have changed OneSignal.Notifications.requestPermission(true); to OneSignal.Notifications.canRequest(); and the dialog haven't shown again. I'm unsure whether this is a good practice or not.