Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question]: Disable In app Push Notification with new 5.x version #2097

Closed
1 task done
simranthakkar1996 opened this issue May 27, 2024 · 12 comments
Closed
1 task done

Comments

@simranthakkar1996
Copy link

simranthakkar1996 commented May 27, 2024

How can we help?

I was following the OneSignal migration guide
https://github.com/OneSignal/OneSignal-Android-SDK/blob/user-model/main/MIGRATION_GUIDE.md

And I have to disable in app message
With the older version we had a parameter (selection) boolean that we were using as parameter of disabling in app push notification.
With the version update I updated the code and according to the migration we can use

boolean getPaused()
void setPaused(boolean value)

but I was just thinking of how we can provide the user selection boolean value to the optOut method that we have in this new version

OneSignal.promptForPushNotifications(false,
                    selection -> {
                        OneSignal.disablePush(!selection);
                    });

Thank you so much for the help

Code of Conduct

  • I agree to follow this project's Code of Conduct
@simranthakkar1996 simranthakkar1996 changed the title [question]: Disable Push with new 5.x version [question]: Disable In app Push Notification with new 5.x version May 27, 2024
@jkasten2
Copy link
Member

@simranthakkar1996 The replacement for OneSignal.disablePush(true) is OneSignal.getUser().getPushSubscription().optOut()

@simranthakkar1996
Copy link
Author

simranthakkar1996 commented May 28, 2024

@jkasten2 I had a doubt so I understand we can use
OneSignal.getUser().getPushSubscription().optOut()
But we would need the user selection right when we prompt the notification
So my question was the prompt that we have to give is changed I think

Something that we tried doing is

 OneSignal.getNotifications().requestPermission(Continue.with(r -> {
                if (r.isSuccess()) {
                    if (r.getData()) {
                        // User accepted the permission
                        OneSignal.getUser().getPushSubscription().optIn();
                    } else {
                        // User rejected the permission
                        OneSignal.getUser().getPushSubscription().optOut();
                    }
                } else {
                    // Handle failure
                    VFLog.e(TAG, "One-Signal Permission request failed");
                }
            }));

But there is some inconsistency before getData is object parameter
So any feedback would be helpful

@jkasten2
Copy link
Member

@simranthakkar1996 it seems your orignal code was accounting for ensuring push was enabled after permission was accepted,
as it sounds like you call OneSignal.disablePush(true) in other parts of your code.

With 5.x.x you can simply call optIn() and optOut() and this state will be taken care of for you with the SDK. Calling optIn() will prompt the end-user for notification permission if needed, so these are the only two methods you need to use.

@simranthakkar1996
Copy link
Author

simranthakkar1996 commented May 28, 2024

So just optIn() and optOut() will do the trick
We just are checking that if user have permission enabled or not and based on that we are adding condition to optIn() or optOut()
If it makes sense. Any feedback matters
Thank you so much

boolean pushPermission = OneSignal.getNotifications().getCanRequestPermission();
        if (pushPermission) {
            OneSignal.getUser().getPushSubscription().optIn();
        } else {
            OneSignal.getUser().getPushSubscription().optOut();
        }

@jkasten2
Copy link
Member

@simranthakkar1996 If you simply have an option in your app to turn push notifications on and off then you can simply call optOut() or optIn() depending on what the user wants. If you want to know what the current status is call getOptedIn() so you can show that in your UI.

If there is more to it than that please explain in more detail.

@simranthakkar1996
Copy link
Author

simranthakkar1996 commented May 29, 2024

@jkasten2 Actually we wanna show the dialog and based on user selection we wanna either optIn or optOut
That's why we update the method mentioned in the migration guide from

 OneSignal.promptForPushNotifications(false,
                     selection -> {
                         VFPreference.get().putBoolean(PUSH_NOTIFICATION_PERMISSION_DIALOG_PRESENTED, true);
                         OneSignal.disablePush(!selection);
                     });

To now using Continue

boolean pushPermission = OneSignal.getNotifications().getCanRequestPermission();
if (pushPermission) {
            // Request push notification permission prompt
            OneSignal.getNotifications().requestPermission(Continue.with(r -> {
                if (r.isSuccess()) {
                    if (r.getData()) {
                        // User accepted the permission
                        OneSignal.getUser().getPushSubscription().optIn();
                    } else {
                        // User rejected the permission
                        OneSignal.getUser().getPushSubscription().optOut();
                    }
                } else {
                    // Handle failure
                    VFLog.e(TAG, "One-Signal Permission request failed");
                }
            }));
        }

There is just one issue with this new approach that r.getData() is giving an issue that its object and we are expecting boolean type.

@simranthakkar1996
Copy link
Author

simranthakkar1996 commented May 30, 2024

Okay @jkasten2 I think I have figured it out I was referring to this document
https://documentation.onesignal.com/docs/mobile-sdk#push-notifications

And they had incorrect as requestPermission was missing a boolean parameter

 OneSignal.getNotifications().requestPermission(Continue.with(r -> { ---------------> was missing a boolean parameter here
                if (r.isSuccess()) {
                    if (r.getData()) {
                        // User accepted the permission
                        OneSignal.getUser().getPushSubscription().optIn();
                    } else {
                        // User rejected the permission
                        OneSignal.getUser().getPushSubscription().optOut();
                    }
                } else {
                    // Handle failure
                    VFLog.e(TAG, "One-Signal Permission request failed");
                }
            }));

Its good in this document
https://github.com/OneSignal/OneSignal-Android-SDK/blob/user-model/main/MIGRATION_GUIDE.md

@jkasten2
Copy link
Member

@simranthakkar1996 Ah I see, thanks for pointing out the documentation mistake. We will get that updated.

@goncalo-oliveira
Copy link

I actually have a toggle in the app to enable/disable push notifications. As suggested above, I'm calling optIn when enabled and optOut when disabled.

Nonetheless, after calling optOut I am still receiving push notifications sent from the dashboard (test). Unless there's an exception for messages sent for tests, this isn't the behaviour I was expecting. Can anyone comment?

@nan-li
Copy link
Contributor

nan-li commented Jan 8, 2025

Hi @goncalo-oliveira, I don't think you should receive them if you choose "Send Test Push" from the dashboard. Can you confirm that you do not receive if you send as "regular push"? Does your subscription show as "Unsubscribed Subscriber Opted Out" in the dashboard?

@goncalo-oliveira
Copy link

@nan-li thanks for replying.

Does your subscription show as "Unsubscribed Subscriber Opted Out" in the dashboard?

No, the subscription continues to show as "Subscribed" after calling optOut.

@goncalo-oliveira
Copy link

Ok, I'm not sure what happened, but it's working now. I reset the simulator device (which rotated the device's uuid) and it worked as expected.

  • Initial state is unsubscribed
  • Push message not delivered
  • Enabling (optIn) changes the state to subscribed
  • Push message delivered
  • Disabling (optOut) changes the state to unsubscribed (although it takes a few seconds)
  • Push message not delivered

The code didn't change, so I'm not sure what happened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants