-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[firebase_messaging] Add support for handling messages in background #38
Conversation
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public class FlutterFirebaseMessagingService extends FirebaseMessagingService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We, as clients of Firebase SDK, do not control the create and stop of this service. So I would recommend to not execute asynchronous/long term operations within this class.
I would recommend to start a new service where you, as plugin developer, can control the calls to Service.stop()
. Just make sure to send the data you need to that service.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a good point. Instead of creating a new service, could we use a JobScheduler to perform the necessary work required for process the message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JobScheduler will require you to write a service.. So.. I think it is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually meant to use WorkManager which I believe does not require a separate service.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WorkManager workers can be triggered at most 15 min interval. Service is a better abstraction as it does not have that limitation. A client might receive the push token and right way a push notification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In situations where the app is in the background Android does not allow the creation of background services.
With Android 8.0, there is a complication; the system doesn't allow a background app to create a background service.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh god...
Options:
- Leave as it is and let's see if it breaks.
- Start a foreground service on Android +8.0.
WorkManager and JobScheduler will delay the execution, so I don't think they are a good option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with option 1, I will add a note to the README indicating that background handling should be quick since background tasks are limited.
// If another thread is waiting, then wake that thread when the callback returns a result. | ||
MethodChannel.Result result = null; | ||
if (latch != null) { | ||
result = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about to create a new class LatchResult
to wrap this inlined logic? Just passing an instance of LatchResult
or null would have the same result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
private MethodChannel.Result result; | ||
|
||
public LatchResult(final CountDownLatch latch) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My initial suggestion, I thought of something like
public class LatchResult extends MethodChannel.Result {
final CountDownLatch latch;
public LatchResult(final int latchCount){
this.latch = new CountDownLatch(latchCount);
}
}
Same feature has been developed for iOS. See #53 |
@kroikie That was the plan. Let's get this PR done first ;) |
How do I try the latest changes on this branch Ive been using this the whole time before migration of the plugin to flutterfire Anyone know what I can use now? |
@XSiyabonga |
266ef40
to
37d4156
Compare
I tried this, but got a strange error
|
@kdy1 that is strange, let me take a look. |
@kdy1 Thanks for flagging that, it should be resolved now. |
@kroikie How can I test this? ( |
@kdy1 The service that receives the messages while the app is in the background is managed by Google Play Services, so it may not show up with other developer managed services. Note: onBackgroundMessage will only be triggered when a data-message is received while the app is not in the foreground. |
@kroikie Thank you for detail. I was trying to catch notification message which also has data. |
Will this work only when the app is in the background, or even if it's killed or not in memory? |
Hi @kroikie , I'm kind of confuse, does this merge resolve data message on app terminated problem, as on https://pub.dev/packages/firebase_messaging, it still showing unsupported for data message Thank you for your answer and attentions |
Description
When Flutter app is in the background or terminated allow it to handle incoming FCM messages. This update uses a similar strategy to the one used by the android_alarm_manager plugin. When the application starts an additional background channel is started to handle incoming messages when the app is not in the foreground.
Migration of flutter/plugins#1900
Related Issues
flutter/flutter#32372
Remaining work
TODOs before this can be merged
fcmSetupBackgroundChannel
to a static void method