EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 441 forks source link

onMessageReceivedCallback works in foreground and background but not if app is closed (e.g. swiped from recents) #632

Open trashbytes opened 6 years ago

trashbytes commented 6 years ago

The onMessageReceivedCallback handler works just fine when the app is running in foreground or when I minimize it. Whenever I close it, for example by swiping it from the recents, it will not work anymore.

When it's in this state the only notifications that come through (to the tray) are these kind:

notification: {
  title: 'Title',
  body: 'Body'
}

But what I need is this:

data = {
  messageId: '1519656340496',
  userId: 'USER_ID',
  date: '1519656340496',
  text: 'Text Message',
  type: 'message',
  messageCollectionId: 'messages',
  channelId: 'CHANNEL_ID',
  displayName: 'USER_NAME'
}

However the app only receives them after launching the app.

This line is already active in the include.gradle file:

compile "com.google.firebase:firebase-messaging:$firebaseVersion"

Both services are enabled in AppManifest.xml:

<service android:name="org.nativescript.plugins.firebase.MyFirebaseInstanceIDService">
  <intent-filter>
    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
  </intent-filter>
</service>
<service android:name="org.nativescript.plugins.firebase.MyFirebaseMessagingService">
  <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
  </intent-filter>
</service>

onMessageReceivedCallback is initialized directly in the .init() function in app.js. I'm sending to a topic which the user subscribes to when he logs in.

Other things that might be useful to know for debugging: I've also added these parameters

android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop"
trashbytes commented 6 years ago

@EddyVerbruggen any idea?

EddyVerbruggen commented 6 years ago

Not really. Perhaps move that init logic out of app.js, and keep the require of the plugin in there.

trashbytes commented 6 years ago

You mean moving the whole firebase.init() out of app.js but still leaving require('nativescript-plugin-firebase')?

EddyVerbruggen commented 6 years ago

yes..

trashbytes commented 6 years ago

This does not work. Is it even remotely possible that onMessageReceivedCallback is called when the app is suspended? Or do I have to use 'notification' instead of 'data' if I want to receive notifications in that state?

erkanarslan commented 6 years ago

@trashbytes I searched about that problem and found that firebase won't notify your app for a new notification if user closed the app by swiping. They say that user closes the app consciously if he is swiping it and they respect that. So, they don't notify your app. I think there is nothing else to do about that.

mayureshjadhav commented 6 years ago

+1

KkevinLi commented 6 years ago

@erkanarslan I do not believe this is true. Using the Quickstart Android demo (https://github.com/firebase/quickstart-android/blob/master/messaging/README.md) I am able to get onMessageReceived() even after swiping the app and from there I can build local notifications to show the user.

On the other hand, even with the latest 6.20 nativescript plugin, onMessageReceived() will not be called for me once I swipe the app. My manifest is identical to the one from Android Quickstart and get the same behavior as @trashbytes.

@EddyVerbruggen Is this the expected behavior and if so why does it deviate from original FCM behavior? I have replicated this using the demo and a new app with the plugin installed. Devices used : s8, One plus 3T, Pixel and a Moto on OS versions 24 & 26.

trashbytes commented 6 years ago

Thanks for sharing @KkevinLi ! Please keep us updated if you make any advancements regarding this. For us it's not currently a priority but still on the list of things which we want to get working so that we can achieve richer notifications.

caiusCitiriga commented 5 years ago

@KkevinLi any news on this?

I'm experiencing this issue too. I've also tried to create a fresh NGx blank project to test it in a clean environment, but the issue still occurs.

Exactly how explained by @trashbytes. In my opinion, according to what the documentation states, we should be able to reach that callback even if the app is intentionally closed by the user, with the only difference that no visual notification will be shown in the tray.

An interesting fact is that when using the nativescript-background-geolocation-lt library, without enabling the sync but setting the service as foreground, if I send the notification with only the data payload (no title or body) works perfectly even with the app closed.

But this is a paid library and a bit huge as "workaround" since it introduces a lot of other "headaches".

@EddyVerbruggen I'm kinda stuck here 😅. Can we hope in a fix or at least a review of the problem by you or should we look for other solutions?

I'd love to help somehow, but the fact that I'm relying on Nativescript proves my noobishness on native code 😔

marianadgeorgieva2 commented 5 years ago

Hi. Is there any update on this?

dangersvn commented 5 years ago

Hi @EddyVerbruggen , I got the same issue. It is working perfectly when the app in Background or in Foreground but when the app exit then i receive nothing.

Thank you!

erkanarslan commented 5 years ago

@dangersvn If you are sending a data message and the application is closed, you won't get a notification in notification tray. Try sending a notification message. If you are already sending a notification message, some devices seems to be preventing FCM messages to preserve the battery. Try with another device. If you are trying with an iPhone simulator, as far as I know you cannot get a notification for ios simulators.

dangersvn commented 5 years ago

Hi @erkanarslan Thank you very much for your feedback! I tried with samsung s8 device + firebase + postman to verify the different between the Data message and Notification message and exactly as you described. I have not tested with iOS simulator yet so no feedback. In my case i have to deal with Data message, since the app depends on a third party backend which always using Data message to send PN via firebase to users while he/she offline. In case can't modify the third party backend then i need to use a service that is running on background even when the app is closed to deal with Data message.

hkurma commented 5 years ago

@erkanarslan When my app is closed and I send a notification message I do receive the notification in the notification tray, but the onMessageReceivedCallback() is not called (I know this since my console.log statement did log anything). Is it the desired behaviour ?

erkanarslan commented 5 years ago

@hkurma This the normal behavior. When you tap the notification, onMessageReceivedCallback() should be called.

gustavost26 commented 2 years ago

Still don't have any concrete solution to this problem?