OneSignal-Unity-SDK icon indicating copy to clipboard operation
OneSignal-Unity-SDK copied to clipboard

[Bug]: com.onesignal.OneSignal$PromptForPushNotificationPermissionResponseHandler.response

Open shefich opened this issue 2 years ago • 9 comments

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?

Here's a code I am using to get the issue:
'void Start () {		
		#if UNITY_IOS
				OneSignal.Default.Initialize("xxxx");
		#elif UNITY_ANDROID
				OneSignal.Default.Initialize("xxxx");
		#endif

		PromptForPush();
		OneSignal.Default.SetLaunchURLsInApp(false);
    }

public async void PromptForPush() {
        var result = await OneSignal.Default.PromptForPushNotificationsWithUserResponse();
    }
'

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

main (native)
tid=1 systid=31868
Triggered ANR
Root blocking
0
libc.so
nanosleep + 8
1
libil2cpp.so
GC_lock
2
libil2cpp.so
GC_register_my_thread
3
libil2cpp.so
il2cpp::gc::GarbageCollector::RegisterThread(void*)
4
libil2cpp.so
il2cpp::vm::Thread::Attach(Il2CppDomain*)
5
libunity.so
scripting_thread_attach(ScriptingDomainPtr)
6
libunity.so
JavaToScriptingThreadAttach::JavaToScriptingThreadAttach(ScriptingDomainPtr)
7
libunity.so
UnityJavaProxy_invoke(_JNIEnv*, _jobject*, long, _jstring*, _jobjectArray*)
8
libart.so
art_quick_generic_jni_trampoline + 148
9
libart.so
nterp_helper + 1948
10
data@app@[email protected][email protected]@classes.vdex
com.unity3d.player.ReflectionHelper.a
11
libart.so
nterp_helper + 52
com.unity3d.player.ReflectionHelper.nativeProxyInvoke (Native method)
com.unity3d.player.ReflectionHelper.a (unavailable)
com.unity3d.player.ReflectionHelper$1.invoke (unavailable:29)
java.lang.reflect.Proxy.invoke (Proxy.java:1006)
com.onesignal.OneSignal$PromptForPushNotificationPermissionResponseHandler.response (OneSignal.java)
com.onesignal.NotificationPermissionController.fireCallBacks (NotificationPermissionController.kt:115)
com.onesignal.NotificationPermissionController.onAccept (NotificationPermissionController.kt:80)
com.onesignal.PermissionsActivity$1.run (PermissionsActivity.java:149)
android.os.Handler.handleCallback (Handler.java:942)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loopOnce (Looper.java:226)
android.os.Looper.loop (Looper.java:313)
android.app.ActivityThread.main (ActivityThread.java:8757)
java.lang.reflect.Method.invoke (Native method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

shefich avatar May 23 '23 19:05 shefich

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

shepherd-l avatar May 24 '23 17:05 shepherd-l

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

shefich avatar May 24 '23 18:05 shefich

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

shefich avatar May 26 '23 11:05 shefich

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 avatar May 26 '23 14:05 shepherd-l

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

shefich avatar May 26 '23 14:05 shefich

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 avatar May 26 '23 15:05 shepherd-l

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

shefich avatar May 29 '23 16:05 shefich

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

shepherd-l avatar May 29 '23 17:05 shepherd-l

everything is now messed up

rakshitbharat avatar Jan 18 '24 15:01 rakshitbharat