hansemannn / titanium-firebase-cloud-messaging

Use the Firebase Cloud Messaging SDK in Axway Titanium 🚀 Edit
Other
43 stars 31 forks source link

fix: (android) fix checkSelfPermission error on android 13+ #154

Closed jordanbisato closed 3 months ago

jordanbisato commented 3 months ago

Looking in my Crashlytics console of my production version, i saw a lot of errors like this on Android 13+:

Attempt to invoke virtual method 'int android.app.Activity.checkSelfPermission(java.lang.String)' on a null object reference(onToken(e):fcm.registerForPushNotifications();:1292)

When i look for the Titanium code, i see TiApplication.getAppCurrentActivity() returns an Activity, but on source code from Android, an Activity do not have the checkSelfPermission method on it.

Searching on google, i found that Context have the method implemented. https://developer.android.com/reference/android/content/Context#checkSelfPermission(java.lang.String)

I tested on my Android 14 device and it works normally to receive tokens and push.

firebase.cloudmessaging-android-3.4.2.zip

m1ga commented 3 months ago

Not having that issue but I'll test it in the next days. Where do you call registerForPushNotifications in your app? Do you run it in alloy.js? That could cause issues

jordanbisato commented 3 months ago

I call it on login page (first after index) as the last thing on postLayout event.

m1ga commented 3 months ago

Strange. It is working without that error on my Pixel 7, Android 14 phone.

Ti.Network.registerForPushNotifications({
    success: function() {
        fcm.registerForPushNotifications();
    },
    error: function(e) {},
    callback: function() {}
});

Even if I just use fcm.registerForPushNotifications(); without the permission check around it it will just show the warning that I need to ask for permissions. Android Studio says that TiApplication.getAppCurrentActivity() is converted to an ContextWrapper so it does find checkSelfPermission.

But I'll test your PR and if that behaves the same it might be a better option if some devices have issues with that.

m1ga commented 3 months ago

I think since it says on a null object in your error that the activity was gone already when the method was called. As the SDK uses Activity currentActivity = TiApplication.getInstance().getCurrentActivity(); for the permission check too (source) I'm fine with the change. It works the same in my test app.

But you might still see that error e.g. if users close the login window right away or so.