Integrating Firebase C++ SDK without using NativeActivity

205 views
Skip to first unread message

Woody Guo

unread,
Apr 15, 2017, 1:46:21 AM4/15/17
to Firebase Google Group
So, we are developing an App for both Android and iOS, and we want to send notifications using Firebase Cloud Messaging, i.e. FCM.

I managed to get notifications sent from the Firebase Console by following the quick guide using a NativeActivity.

However, as this is a normal App, it's not a game written purely using C++. We build the UI using Java for Android and Objective-C for iOS. We then use C++ to implement common utils and business logics so they are written once and built for both platforms. So there's not a NativeActivity involved here.

I thought I should be able to call a native function from my Java Activity to initialize FCM and start to wait for notifications to come. But I'm not sure if this is feasible. And, what will happen if the Activity passed as a parameter to initialize the Firebase::App is finished? Will I still be able to receive notifications?

Any help would be greatly appreciated.

Thanks!

Alex Ames

unread,
Apr 17, 2017, 2:38:41 PM4/17/17
to Firebase Google Group
If I am understanding your use case correctly, I believe the C++ library should work just fine for your situation.

The service that runs in the background is what actually collects the incoming messages for your app. If your app is running in the foreground the message is sent directly to the running app. Otherwise a notification is popped up in your system tray. The activity isn't needed for this.

The activity is only needed by the underlying C++ library code to retrieve the intent that started the activity (as sometimes FCM message payloads are passed to the Activity via an Intent). As long as you are initializing Firebase Messaging every time your main activity starts up you should be fine. During initialization the Activity pointer is initialized and the FCM C++ library can check which intent started the new Activity, in case it needs to respond to a new incoming message.

If you have any issues getting this to work or further questions let me know.

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/eb7a048f-1b4a-4e3f-bbf4-298404668fe4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Woody Guo

unread,
Apr 18, 2017, 9:40:20 AM4/18/17
to Firebase Google Group
Thanks Alex for your information!

Just one thing to confirm before I start coding, will I receive data messages if our main activity is finished so our App is running in the background, or our App has already been killed by system as it's an empty process?

We are using only data messages because we want our App to display notifications.

I understand that, the FCM C++ library is not initialized and there is no C++ message listener registered because our App is not running in the foreground. So, when Google Play Services receives a data message, it broadcasts the message, the FirebaseInstanceIdReceiver receives the message, and then what will happen?

If I do not de-initialize the FCM C++ library when the main activity finishes, will it cause memory leak?

在 2017年4月18日星期二 UTC+8上午2:38:41,Alex Ames写道:
If I am understanding your use case correctly, I believe the C++ library should work just fine for your situation.

The service that runs in the background is what actually collects the incoming messages for your app. If your app is running in the foreground the message is sent directly to the running app. Otherwise a notification is popped up in your system tray. The activity isn't needed for this.

The activity is only needed by the underlying C++ library code to retrieve the intent that started the activity (as sometimes FCM message payloads are passed to the Activity via an Intent). As long as you are initializing Firebase Messaging every time your main activity starts up you should be fine. During initialization the Activity pointer is initialized and the FCM C++ library can check which intent started the new Activity, in case it needs to respond to a new incoming message.

If you have any issues getting this to work or further questions let me know.
On Fri, Apr 14, 2017 at 9:23 PM, Woody Guo <wood...@gmail.com> wrote:
So, we are developing an App for both Android and iOS, and we want to send notifications using Firebase Cloud Messaging, i.e. FCM.

I managed to get notifications sent from the Firebase Console by following the quick guide using a NativeActivity.

However, as this is a normal App, it's not a game written purely using C++. We build the UI using Java for Android and Objective-C for iOS. We then use C++ to implement common utils and business logics so they are written once and built for both platforms. So there's not a NativeActivity involved here.

I thought I should be able to call a native function from my Java Activity to initialize FCM and start to wait for notifications to come. But I'm not sure if this is feasible. And, what will happen if the Activity passed as a parameter to initialize the Firebase::App is finished? Will I still be able to receive notifications?

Any help would be greatly appreciated.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

Alex Ames

unread,
Apr 18, 2017, 2:18:28 PM4/18/17
to Firebase Google Group
The availability of the data payload follows the standard rules, as outlined on this page:

  • When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
  • When in the foreground, your app receives a message object with both payloads available.
In C++ the way this works is that when FCM is initialized, during the initialization the library checks the Intent that started the Activity. If it was started by a notification with a data payload, it will extract the data payload from the Intent and forward it to the OnMessage callback function just like messages that were received while the app is in the foreground.

If I do not de-initialize the FCM C++ library when the main activity finishes, will it cause memory leak?

When the Activity that owns the loaded native library is shut down it will take the memory with it. You shouldn't have a memory leak, but it's generally good practice to call Terminate if you know your app is closing.


To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages