hansemannn / titanium-firebase-cloud-messaging

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

Error and crash when receive a push in foreground #143

Closed jordanbisato closed 1 year ago

jordanbisato commented 1 year ago

Hi,

When i receive a push in foreground on android using the 3.3.1 released version (and with 3.3.1 version from m1g4 too https://github.com/hansemannn/titanium-firebase-cloud-messaging/pull/137) i receive an error:

[DEBUG] FirebaseMsgService: Message Notification Body: TEST
[ERROR] V8Exception: Exception occurred at undefined:1050: Uncaught Error: Attempt to get length of null array
[DEBUG] AndroidRuntime: Shutting down VM

I put some logs into it to get the exactly location of error and it occours on CloudMessagingModule.java inside onMessageReceived in this line: fireEvent("didReceiveMessage", data);

It's possible give a look on it? Thanks!

Using an Xiaomi Mi 9T with Android 10 to test. SDK 11.0.0.GA

m1ga commented 1 year ago

can you say what is null? Since that part is in a try/catch it should end up in message exception if that line fails. The V8Exception is thrown by the SDK. Not sure at the moment where it tries to get the length of.

jordanbisato commented 1 year ago

No, i can't discover what is null. I only know it's between the fireEvent on https://github.com/hansemannn/titanium-firebase-cloud-messaging/blob/main/android/src/firebase/cloudmessaging/CloudMessagingModule.java#L239 and before the function of listener in my code.

fcm.addEventListener("didReceiveMessage", onMessage);
function onMessage(e) {
        // Fired for all kind of notifications (foreground, background & closed)
        Ti.API.debug("######### Got message: " + JSON.stringify(e));
        Alloy.Globals.fcm.lastData = e.data;
        Alloy.Globals.fcm.lastData.inBackground = e.inBackground;
    }
m1ga commented 1 year ago

If you can reproduce it please add a Log.i("---", data.toString()); before to see if the data is filled. And Log.i("---", "Message: " + message); before 238. Perhaps its the message that can't be parsed into a KrollDict. That lines hasn't been touched for 4 years (besides the linting in the last version)

hansemannn commented 1 year ago

Maybe it's related to the "data" changes we merged recently? It would be interesting to know if an earlier version works. But yeah, the debug logs will help!

m1ga commented 1 year ago

could be but only if it is a data notification. But since Message Notification Body is called it should be a normal notifiaction and not go into that branch. But will check that

jordanbisato commented 1 year ago

Huum, i noted something: i was sending a Data notification, but it enter in the if of the message notification. In background it works well, but on foreground it crash.

m1ga commented 1 year ago

what are you using to send the notification? And can you show the content?

jordanbisato commented 1 year ago

I'm using the firebase console to send the push. The content of push (in background):

Last data: {"google.original_priority":"high","gcm.n.analytics_data_google.c.a.c_l":"TESTE","google.sent_time":"1661886749988","telaPush":"/aporte/aporte","google.delivered_priority":"high","google.message_id":"0:1661886749998202%452c09d0452c09d0","collapse_key":"br.com.cvpb","gcm.n.analytics_data_google.c.a.ts":"1661886749","gcm.n.analytics_data_google.c.a.c_id":"7234120883046604539","inBackground":true,"google.ttl":"2419200","from":"869891314489","gcm.n.analytics_data_from":"869891314489","gcm.n.analytics_data_google.c.a.e":"1","gcm.n.analytics_data_google.c.a.udt":"0"}

jordanbisato commented 1 year ago

And here the log of module on crash:

[DEBUG] FirebaseMsgService: Message Notification Body: TESTE

[DEBUG] FirebaseCloudMessaging: (Firebase-Messaging-Intent-Handle) [61897,69177] onMessageReceived

[INFO] I/onMessageReceived data: : (Firebase-Messaging-Intent-Handle) [0,69177] {}

[INFO] I/--- : (Firebase-Messaging-Intent-Handle) [1,69178] Message: {messageType=null, data={"telaPush":"\/aporte\/aporte"}, messageId=0:1661886822393990%452c09d0452c09d0, from=869891314489, to=null, title=TESTE, body=TESTE, ttl=2419200, sendTime=1661886822368}

[INFO] I/onMessageReceived data after message: : (Firebase-Messaging-Intent-Handle) [0,69178] {"message":{"messageType":null,"data":{"telaPush":"\/aporte\/aporte"},"messageId":"0:1661886822393990%452c09d0452c09d0","from":"869891314489","to":null,"title":"TESTE","body":"TESTE","ttl":2419200,"sendTime":1661886822368}}

[ERROR] V8Exception: Exception occurred at undefined:1052: Uncaught Error: Attempt to get length of null array

[DEBUG] AndroidRuntime: Shutting down VM
m1ga commented 1 year ago

so like this ? Screenshot_20220830_212615 That works fine here.

I wonder if it is realted to: https://github.com/hansemannn/titanium-firebase-cloud-messaging/pull/137#issuecomment-1144844079 since you have some other analytics data in your notification.

jordanbisato commented 1 year ago

I put a log before this line: https://github.com/hansemannn/titanium-firebase-cloud-messaging/blob/main/android/src/firebase/cloudmessaging/TiFirebaseMessagingService.java#L74

And send a push with data and one without data.

The result:

With data:

[DEBUG]  FirebaseMsgService: remoteMessage.getData(): {telaPush=/aporte/aporte}

[DEBUG]  FirebaseMsgService: remoteMessage.getNotification(): com.google.firebase.messaging.RemoteMessage$Notification@75a163e

[DEBUG]  FirebaseMsgService: Message Notification Body: TESTE

Without data:

[DEBUG]  FirebaseMsgService: remoteMessage.getData(): {}

[DEBUG]  FirebaseMsgService: remoteMessage.getNotification(): com.google.firebase.messaging.RemoteMessage$Notification@a9fa087

[DEBUG]  FirebaseMsgService: Message Notification Body: TESTE

Both entered in the if.

I think we need verify if data is truly, because remoteMessage.getNotification() isn't null in both cases.

m1ga commented 1 year ago

The notification from Firebase is a mixed notification, so it has a notification block and a data part.

So this is my Firebase console push:

Message Notification Body: asdf

Message: {messageType=null, data={"asdf":"234"}, messageId=0:1661888246119579%ac3e80e4ac3e80e4, from=47582397024, to=null, title=asdf, body=asdf, ttl=2419200, sendTime=1661888190127}

KrollDict: {"message":{"messageType":null,"data":{"asdf":"234"},"messageId":"0:1661888246119579%ac3e80e4ac3e80e4","from":"47582397024","to":null,"title":"asdf","body":"asdf","ttl":2419200,"sendTime":1661888190127}}

and this is a data notification using PHP:

Data message: {telaPush=/aporte/aporte, id=1, type=noti/data: data part, badge=5, channelId=my_channel1}

Messge: {messageType=null, data={"badge":"5","id":"1","type":"noti\/data: data part","telaPush":"\/aporte\/aporte","channelId":"my_channel1"}, messageId=0:1661888210990602%ac3e80e4f9fd7ecd, from=47582397024, to=null, ttl=2419200, sendTime=1661888210973}

KrollDict: {"message":{"messageType":null,"data":{"badge":"5","id":"1","type":"noti\/data: data part","telaPush":"\/aporte\/aporte","channelId":"my_channel1"},"messageId":"0:1661888210990602%ac3e80e4f9fd7ecd","from":"47582397024","to":null,"ttl":2419200,"sendTime":1661888210973}}

Both cases don't crash for me and all of them have the data node.

m1ga commented 1 year ago

I've found the issue :rofl: your log: Ti.API.debug("######### Got message: " + JSON.stringify(e));

remove that :wink:

[ERROR] TiExceptionHandler: (main) [3,108462] /alloy/controllers/index.js:290
[ERROR] TiExceptionHandler: Ti.API.debug("######### Got message: "+JSON.stringify(e)),
[ERROR] TiExceptionHandler:                                             ^
[ERROR] TiExceptionHandler: Error: Attempt to get length of null array
[ERROR] TiExceptionHandler:     at JSON.stringify (<anonymous>)
[ERROR] TiExceptionHandler:     at CloudMessaging.onMessage (/alloy/controllers/index.js:290:45)
[ERROR] TiExceptionHandler:     at CloudMessaging.value (ti:/kroll.js:1604:27)
[ERROR] TiExceptionHandler:     at CloudMessaging.value (ti:/kroll.js:1656:25)
[ERROR] TiExceptionHandler:
[ERROR] TiExceptionHandler:     ti.modules.titanium.android.notificationmanager.NotificationChannelProxy.getVibrationPattern(NotificationChannelProxy.java:225)
[ERROR] TiExceptionHandler:     org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method)
[ERROR] TiExceptionHandler:     org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:63)
[ERROR] TiExceptionHandler:     org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:985)
[ERROR] TiExceptionHandler:     org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1219)
[ERROR] TiExceptionHandler:     android.os.Handler.dispatchMessage(Handler.java:102)
[ERROR] TiExceptionHandler:     android.os.Looper.loopOnce(Looper.java:201)
[ERROR] TiExceptionHandler:     android.os.Looper.loop(Looper.java:288)
[ERROR] TiExceptionHandler:     android.app.ActivityThread.main(ActivityThread.java:7898)
[ERROR] TiExceptionHandler:     java.lang.reflect.Method.invoke(Native Method)
[ERROR] V8Exception: Exception occurred at /alloy/controllers/index.js:290: Uncaught Error: Attempt to get length of null array
jordanbisato commented 1 year ago

OMG. THAT'S IT!!! Hahahaha Worked now! Thanks again @m1ga

m1ga commented 1 year ago

But why didn't it show you that log but only the V8 part :thinking:

jordanbisato commented 1 year ago

And how i can get the data in a foreground notification? Because JSON.stringify(e) crash, and if i log the e.data or e.inBackground, both are undefined.

m1ga commented 1 year ago

I use

if (OS_ANDROID) {
        console.log("Got message: " + JSON.stringify(e.message));
        alert("Got message: " + JSON.stringify(e.message.data));
    } else {
        var data = JSON.parse(e.data || '');
        console.log("Got message: " + JSON.stringify(data));
        alert("Got message: " + JSON.stringify(data));
    }

in my test app

jordanbisato commented 1 year ago

It worked like a charm! You're the man. Thank you.