-
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] Handle background messages on iOS #2016
Closed
Closed
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8e841b3
added ios support
tobiasjunsten 4d0844a
- renamed variable and removed commented log
tobiasjunsten 3ca64a2
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
tobiasjunsten 08dcf2e
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
tobiasjunsten 44b68ba
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
tobiasjunsten a87c807
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
tobiasjunsten 79e3fa8
Merge branch 'master' of github.com:FirebaseExtended/flutterfire into…
tobiasjunsten 07fc453
[firebase_messaging] Added instructions for ios background handling i…
tobiasjunsten 1824a2a
[firebase_messaging] Fixed some formatting
tobiasjunsten 0225706
[firebase_messaging] Formatted the objective-c code
tobiasjunsten 24cbd6f
Merge master into ios-background-support
tobiasjunsten 34ad709
[firebase_messaging] Fixed readme after merge
tobiasjunsten afcb41b
[firebase_messaging] updated versions
tobiasjunsten 7132d34
Merge branch 'master' into ios-background-support
tobiasjunsten 566a977
[firebase_messaging] Added import firebase_messaging to the readme.
tobiasjunsten 685f549
Merge branch 'master' into ios-background-support
tobiasjunsten 9d495bf
[firebase_messaging] Updated readme with suggestions from @oskara
tobiasjunsten 43deb43
Merge branch 'master' into ios-background-support
tobiasjunsten 5b71c9a
- fixed spelling mistake in readme
tobiasjunsten e31f2cd
Merge branch 'master' into ios-background-support
tobiasjunsten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,8 +52,46 @@ Note: When you are debugging on Android, use a device or AVD with Google Play se | |
<category android:name="android.intent.category.DEFAULT" /> | ||
</intent-filter> | ||
``` | ||
#### Optionally handle background messages | ||
|
||
### iOS Integration | ||
|
||
To integrate your plugin into the iOS part of your app, follow these steps: | ||
|
||
1. Generate the certificates required by Apple for receiving push notifications following [this guide](https://firebase.google.com/docs/cloud-messaging/ios/certs) in the Firebase docs. You can skip the section titled "Create the Provisioning Profile". | ||
|
||
1. Using the [Firebase Console](https://console.firebase.google.com/) add an iOS app to your project: Follow the assistant, download the generated `GoogleService-Info.plist` file, open `ios/Runner.xcworkspace` with Xcode, and within Xcode place the file inside `ios/Runner`. **Don't** follow the steps named "Add Firebase SDK" and "Add initialization code" in the Firebase assistant. | ||
|
||
1. In Xcode, select `Runner` in the Project Navigator. In the Capabilities Tab turn on `Push Notifications` and `Background Modes`, and enable `Background fetch` and `Remote notifications` under `Background Modes`. | ||
|
||
1. Follow the steps in the "[Upload your APNs certificate](https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_certificate)" section of the Firebase docs. | ||
|
||
1. If you need to disable the method swizzling done by the FCM iOS SDK (e.g. so that you can use this plugin with other notification plugins) then add the following to your application's `Info.plist` file. | ||
|
||
```xml | ||
<key>FirebaseAppDelegateProxyEnabled</key> | ||
<false/> | ||
``` | ||
|
||
After that, add the following lines to the `(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` | ||
method in the `AppDelegate.m`/`AppDelegate.swift` of your iOS project. | ||
|
||
Objective-C: | ||
```objectivec | ||
if (@available(iOS 10.0, *)) { | ||
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self; | ||
} | ||
``` | ||
|
||
Swift: | ||
```swift | ||
if #available(iOS 10.0, *) { | ||
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate | ||
} | ||
``` | ||
|
||
### Handle background messages (Optional) | ||
|
||
#### Android configuration | ||
>Background message handling is intended to be performed quickly. Do not perform | ||
long running tasks as they may not be allowed to finish by the Android system. | ||
See [Background Execution Limits](https://developer.android.com/about/versions/oreo/background) | ||
|
@@ -110,6 +148,22 @@ By default background messaging is not enabled. To handle messages in the backgr | |
<application android:name=".Application" ...> | ||
``` | ||
|
||
#### iOS configuration (Swift) | ||
1. In the top of `AppDelegate.swift`, add the import of firebase_messaging: | ||
|
||
```swift | ||
import firebase_messaging | ||
``` | ||
|
||
1. Then add the following code to `AppDelegate.swift`: | ||
|
||
```swift | ||
FLTFirebaseMessagingPlugin.setPluginRegistrantCallback({ (registry: FlutterPluginRegistry) -> Void in | ||
GeneratedPluginRegistrant.register(with: registry); | ||
}); | ||
``` | ||
|
||
#### Usage in the common Dart code | ||
1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages | ||
|
||
```dart | ||
|
@@ -155,41 +209,6 @@ By default background messaging is not enabled. To handle messages in the backgr | |
so that it can be ready to receive messages as early as possible. See the | ||
[example app](https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/example) for a demonstration. | ||
|
||
### iOS Integration | ||
|
||
To integrate your plugin into the iOS part of your app, follow these steps: | ||
|
||
1. Generate the certificates required by Apple for receiving push notifications following [this guide](https://firebase.google.com/docs/cloud-messaging/ios/certs) in the Firebase docs. You can skip the section titled "Create the Provisioning Profile". | ||
|
||
1. Using the [Firebase Console](https://console.firebase.google.com/) add an iOS app to your project: Follow the assistant, download the generated `GoogleService-Info.plist` file, open `ios/Runner.xcworkspace` with Xcode, and within Xcode place the file inside `ios/Runner`. **Don't** follow the steps named "Add Firebase SDK" and "Add initialization code" in the Firebase assistant. | ||
|
||
1. In Xcode, select `Runner` in the Project Navigator. In the Capabilities Tab turn on `Push Notifications` and `Background Modes`, and enable `Background fetch` and `Remote notifications` under `Background Modes`. | ||
|
||
1. Follow the steps in the "[Upload your APNs certificate](https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_certificate)" section of the Firebase docs. | ||
|
||
1. If you need to disable the method swizzling done by the FCM iOS SDK (e.g. so that you can use this plugin with other notification plugins) then add the following to your application's `Info.plist` file. | ||
|
||
```xml | ||
<key>FirebaseAppDelegateProxyEnabled</key> | ||
<false/> | ||
``` | ||
|
||
After that, add the following lines to the `(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions` | ||
method in the `AppDelegate.m`/`AppDelegate.swift` of your iOS project. | ||
|
||
Objective-C: | ||
```objectivec | ||
if (@available(iOS 10.0, *)) { | ||
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self; | ||
} | ||
``` | ||
|
||
Swift: | ||
```swift | ||
if #available(iOS 10.0, *) { | ||
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate | ||
} | ||
``` | ||
|
||
### Dart/Flutter Integration | ||
|
||
|
@@ -207,12 +226,12 @@ Next, you should probably request permissions for receiving Push Notifications. | |
|
||
Messages are sent to your Flutter app via the `onMessage`, `onLaunch`, and `onResume` callbacks that you configured with the plugin during setup. Here is how different message types are delivered on the supported platforms: | ||
|
||
| | App in Foreground | App in Background | App Terminated | | ||
| --------------------------: | ----------------- | ----------------- | -------------- | | ||
| **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. | | ||
| | App in Foreground | App in Background | App Terminated | | ||
| --------------------------: | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **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. | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this right? Aren't background messages delivered through |
||
|
||
Additional reading: Firebase's [About FCM Messages](https://firebase.google.com/docs/cloud-messaging/concept-options). | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe you should specify where in
AppDelegate.swift
? E.g.didFinishLaunchingWithOptions
if that is the case.