Skip to content
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] Handle background messages on iOS #2016

Closed
wants to merge 20 commits into from

Conversation

tobiasjunsten
Copy link

@tobiasjunsten tobiasjunsten commented Feb 18, 2020

Description

Enable backgound handling on iOS to work the same way as android.

Related Issues

See Issue 47

Related pull request

This pull request was inspired by this one: Pull request 53

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • If the pull request affects only one plugin, the PR title starts with the name of the plugin in brackets (e.g. [cloud_firestore])
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@tobiasjunsten tobiasjunsten changed the title [firebase_messaging] Handle background messages in iOS [firebase_messaging] Handle background messages on iOS Feb 19, 2020
@Finn0811
Copy link

Really looking forward to this, thanks for your work!

@navidonline
Copy link

Thanks for your great work, only worked in background .when app killed not work?

@josh-burton
Copy link
Contributor

@shell32 what device are you testing on? If you force kill an app on iOS (common when testing things like this) then iOS will not deliver push notifications to that app until the app or device is restarted.

@navidonline
Copy link

navidonline commented Feb 27, 2020

@shell32 what device are you testing on? If you force kill an app on iOS (common when testing things like this) then iOS will not deliver push notifications to that app until the app or device is restarted.

iphone 6 , ios 12
ok thanks

@tobiasjunsten
Copy link
Author

@shell32, just as @athornz says the app is not receiving any push notifications if the app has been force-quit. If the app has been suspended by the system (which happens quite fast) the app should be woken up and get some time to process a notification. When I've been testing I've also noticed that it can take a couple of minutes sometimes before the app is woken up if it has been suspended. You can read a bit more here: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application#discussion

@gustavoergalves
Copy link

Looking forward to this, thanks for your work!

@p30arena
Copy link
Contributor

p30arena commented Apr 1, 2020

can this pr solve this issue?
#357

@tobiasjunsten
Copy link
Author

@p30arena No, it only deals with handling push messages when app is in background or suspended state for iOS.

@campioncino
Copy link

campioncino commented Apr 3, 2020

Can I finally see this behaviour in iOS?
7bd39177-e115-4f8f-8aac-a035bcc808f1

@tobiasjunsten
Copy link
Author

@campioncino this pull request is not adding that behaviour. It's just about adding background handling for push messages. So when your app is in the background you'll be able to provide a dart function/isolate that should handle the message.

@campioncino
Copy link

@campioncino this pull request is not adding that behaviour. It's just about adding background handling for push messages. So when your app is in the background you'll be able to provide a dart function/isolate that should handle the message.

I'm sorry, I mean : can I finally do something like the image.

Using onBackgroundMessage: backgroundMessageHandler
can I do something when my app is running in background?

@tobiasjunsten
Copy link
Author

@campioncino Ok :) I guess that should be possible. Anything should be possible in the backgroundMessageHandler. Like fetching data from backend or trigger local notifications.

@fanatic75
Copy link

@isaiahtaylor, please don't post android config on a thread for iOS. There is no point here.

@isaiahtaylorhh
Copy link

isaiahtaylorhh commented Jun 23, 2020

I edited my comment to note that this is not necessarily relevant to this issue, it's just something that you might run into following this path.

@tobiasjunsten
Copy link
Author

@tobiasjunsten , I have tried this pull, so when I add content_available to my payload, the app suddenly loses connection with the console and I cannot read logs anymore.
`{
"to" : "",

 "content_available":true,

"data" : {

 "click_action": "FLUTTER_NOTIFICATION_CLICK",
 "title":"news",
 "body":"body",
 "newsID":200,
 "image_url":"https://media.wired.com/photos/5b899992404e112d2df1e94e/master/pass/trash2-01.jpg",

}
}`

If I remove the content_available:true, it doesn't do anything in the background with this branch. but as soon as I add, it loses connection with the machine and I cannot read further logs to see If I have received the data message

@fanatic75 have you tried the console app on iOS? If you lose the connection with the app in the IDE-console you should still be able to see the logs from the app in the stand alone console app.

@fanatic75
Copy link

@

@tobiasjunsten , I have tried this pull, so when I add content_available to my payload, the app suddenly loses connection with the console and I cannot read logs anymore.
`{
"to" : "",

 "content_available":true,

"data" : {

 "click_action": "FLUTTER_NOTIFICATION_CLICK",
 "title":"news",
 "body":"body",
 "newsID":200,
 "image_url":"https://media.wired.com/photos/5b899992404e112d2df1e94e/master/pass/trash2-01.jpg",

}
}`
If I remove the content_available:true, it doesn't do anything in the background with this branch. but as soon as I add, it loses connection with the machine and I cannot read further logs to see If I have received the data message

@fanatic75 have you tried the console app on iOS? If you lose the connection with the app in the IDE-console you should still be able to see the logs from the app in the stand alone console app.

I have moved from data message to notification message which solves the background problem on IOS as well. OnLaunch is working once the app has been killed and onResume is working when the app is in background. Not using background message callback anymore on both platforms.
Though there is definitely something weird as the connection should not get closed and the app is still running fine without crashing.

| **Notification on Android** | `onMessage` | Notification is delivered to system tray. When the user clicks on it to open app `onResume` fires if `click_action: FLUTTER_NOTIFICATION_CLICK` is set (see below). | Notification is delivered to system tray. When the user clicks on it to open app `onLaunch` fires if `click_action: FLUTTER_NOTIFICATION_CLICK` is set (see below). |
| **Notification on iOS** | `onMessage` | Notification is delivered to system tray. When the user clicks on it to open app `onResume` fires. | Notification is delivered to system tray. When the user clicks on it to open app `onLaunch` fires. |
| **Data Message on Android** | `onMessage` | `onMessage` while app stays in the background. | *not supported by plugin, message is lost* |
| **Data Message on iOS** | `onMessage` | Message is stored by FCM and delivered to app via `onMessage` when the app is brought back to foreground. | Message is stored by FCM and delivered to app via `onMessage` when the app is brought back to foreground. |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? Aren't background messages delivered through onBackgroundMessage after this patch?

import firebase_messaging
```

1. Then add the following code to `AppDelegate.swift`:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should specify where in AppDelegate.swift? E.g. didFinishLaunchingWithOptions if that is the case.

@SKKurz
Copy link

SKKurz commented Jul 10, 2020

is it also possible to use the background message handling on an iOS Simulator? Or is this one of the things that only work on a proper iOS device?

@oskara
Copy link

oskara commented Jul 21, 2020

is it also possible to use the background message handling on an iOS Simulator? Or is this one of the things that only work on a proper iOS device?

As far as I know this will only work on real devices.

@owenkealey
Copy link

Only conflicts are changelog and pubspec? This is a critical merge for my app.

# Conflicts:
#	packages/firebase_messaging/CHANGELOG.md
#	packages/firebase_messaging/pubspec.yaml

Updated version in CHANGELOG.md and pubspec.yaml
@csarigumba
Copy link

Is this schedule for merge? We need this in our app.

| **Notification on Android** | `onMessage` | Notification is delivered to system tray. When the user clicks on it to open app `onResume` fires if `click_action: FLUTTER_NOTIFICATION_CLICK` is set (see below). | Notification is delivered to system tray. When the user clicks on it to open app `onLaunch` fires if `click_action: FLUTTER_NOTIFICATION_CLICK` is set (see below). |
| **Notification on iOS** | `onMessage` | Notification is delivered to system tray. When the user clicks on it to open app `onResume` fires. | Notification is delivered to system tray. When the user clicks on it to open app `onLaunch` fires. |
| **Data Message on Android** | `onMessage` | `onMessage` while app stays in the background. | *not supported by plugin, message is lost* |
| **Data Message on iOS** | `onMessage` | Message is delivered to `onBackgroundMessage`. This is the case both when app is running in background and the system has suspended the app. | Whan app is force-quit by the user the message is not handled. |
Copy link

@LasseRosenow LasseRosenow Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Whan app is force-quit by the user the message is not handled.

You got a typo there. It should be called "When" :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed.

@tobiasjunsten
Copy link
Author

Hi @FirebaseExtended/invertase! Any chance you could take a look at this pull request? It's a really wanted feature for us and (as it seems) a lot of other people.

@tobiasjunsten
Copy link
Author

tobiasjunsten commented Sep 7, 2020

Hi @Salakar! I tried to tag the @FirebaseExtended/invertase team as suggested here: FlutterFire Roadmap but don't seem to work. Any chance any of you could take a look at this pull request?

@kristijandraca
Copy link

Any updates on this?

@nt4f04uNd
Copy link
Contributor

@collinjackson @Ehesp @kroikie @Salakar guys please review this one, we all need it 🙏🥺

@Ehesp
Copy link
Member

Ehesp commented Oct 22, 2020

Hold on to your hats... New dev release coming soon.

# Conflicts:
#	packages/firebase_messaging/CHANGELOG.md
#	packages/firebase_messaging/pubspec.yaml
@nt4f04uNd
Copy link
Contributor

nt4f04uNd commented Oct 29, 2020

@ozcannn14 why would you JSON.stringify your "notification"? If you are sending this with the JS SDK it has to be an object I guess

@gahms
Copy link

gahms commented Oct 29, 2020

I have used this PR and it works. One thing that bit me was that when testing with a local clone of the fork, it seems that firebase_messaging gets initialized before firebase_core and that is not handled very well in the FLTFirebaseMessagingPlugin initWithChannel:registrar:] method that fails silently ([FIRMessaging messaging] is nil).

An entirely different issue is that because of flutter/flutter#14815, I cannot do any of the processing I had in mind in the background isolate and thus it is effectively useless to me to have background support on iOS.

@Salakar Salakar closed this in #4012 Nov 3, 2020
@firebase firebase locked and limited conversation to collaborators Dec 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.