Open shefich opened 1 year ago
Just to clarify, is your issue that the app crashes after calling PromptForPushNotificationsWithUserResponse()
?
What devices is the issue occurring on and what version of Android are they on?
Could you try reproducing the issue and provide the steps when you are able to
I don't know what happened, as I see it is connected to the "PromptForPushNotificationPermissionResponseHandler" and probably somehow connected to the Unity 2021.3.25, but I'm not sure. Android versions: 7,8,9,10,11,12, 13. Devices: Redmi, Motorolla, Samsung, LGE, BLU, and many others
@shepherd-l should PromptForPushNotificationsWithUserResponse() should be called everytime and SDK checks if the popup should be shown? Or there is some code logic to check if we need to ask for prompt?
PromptForPushNotificationsWithUserResponse()
does not contain any code logic to check if we need to prompt the user
We recommend following our guide on how to prompt the user for push permission Push Prompting How to Prompt for Push Permissions with In-App Messages
Let us know if you have any questions!
@shepherd-l thank you for you answer.
I found the needed code example here: https://documentation.onesignal.com/docs/how-to-prompt-for-push-permissions-with-an-in-app-message
OSDeviceState device = OneSignal.getDeviceState(); boolean areNotificationsEnabled = device.areNotificationsEnabled();
Based on your links and the one above the code should look like this:
OneSignal.Default.Initialize("xxx-xxx-xxx-xxx-xxx"); OSDeviceState device = OneSignal.getDeviceState(); boolean areNotificationsEnabled = device.areNotificationsEnabled(); if !areNotificationsEnabled { OneSignal.promptForPushNotifications(); }
Am I right?
getDeviceState()
is a Android SDK method and not available on our Unity SDK
I believe in Unity it would look something like this:
var currentStatus = OneSignal.Default.NotificationPermission;
if (currentStatus == NotificationPermission.NotDetermined) {
OneSignal.Default.PromptForPushNotificationsWithUserResponse();
}
You can see Push Notification Properties code samples here and the types of Notification Permission Statuses here
Sorry for the confusion. That guide code example is specifically for Android. Thanks for bringing it up, we could update our docs to include code samples for our other SDKs.
@shepherd-l your code gives alert: Because this call is not awaited, execution of the current method continues before the call is completed. Probably you need to add await before calling for prompt in order to fix the alert.
var currentStatus = OneSignal.Default.NotificationPermission;
if (currentStatus == NotificationPermission.NotDetermined) {
await OneSignal.Default.PromptForPushNotificationsWithUserResponse();
}
Yes sorry, I forgot to include await
in my code example
PromptForPushNotificationsWithUserResponse()
also returns a NotificationPermission
type
For example :
var response = await OneSignal.Default.PromptForPushNotificationsWithUserResponse();
if (response == NotificationPermission.Authorized) {
// user accepted
}
You can also see our included example MonoBehaviour for some sample usage
Our migration guide also includes some 3.x.x code samples
everything is now messed up
What happened?
Can't reproduce the issue on my device. It started to happen after I switched from unity 2020 to unity 2021.
Steps to reproduce?
What did you expect to happen?
I expected to see allow notification popup probably.
Unity version
2021.3.25 (latest)
OneSignal Unity SDK version
3.0.11
Platform
Android
Relevant log output
Code of Conduct