OneSignal/OneSignal-Android-SDK

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

Closed this issue · 8 comments

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 The replacement for OneSignal.disablePush(true) is OneSignal.getUser().getPushSubscription().optOut()

@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

@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.

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();
        }

@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.

@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.

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

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