OneSignal / OneSignal-Unity-SDK

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

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

Open shefich opened 1 year ago

shefich commented 1 year ago

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@~~CmOdE7AAh1ctuGPMbynBtg==@com.xxx.xxx-0B7YC7vUC3DQXRw1dSbT5A==@base.apk@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

shepherd-l commented 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

shefich commented 1 year ago

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 commented 1 year ago

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

shepherd-l commented 1 year ago

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!

shefich commented 1 year ago

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

shepherd-l commented 1 year ago

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.

shefich commented 1 year ago

@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();
}
shepherd-l commented 1 year ago

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

rakshitbharat commented 5 months ago

everything is now messed up