Skip to content

Commit c588299

Browse files
authored
docs: Update receive.md for FCM doc revamp (#17710)
Update receive.md as part of the FCM doc revamp project
1 parent 063df87 commit c588299

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

docs/cloud-messaging/receive.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ is first important to establish the various states a device can be in:
2525
| **Terminated** | When the device is locked or the application is not running.
2626

2727
There are a few preconditions which must be met before the application can
28-
receive message payloads via FCM:
28+
receive message payloads using FCM:
2929

3030
- The application must have opened at least once (to allow for registration with FCM).
3131
- On iOS, if the user swipes away the application from the app switcher, it must be manually reopened for background messages to start working again.
@@ -37,13 +37,13 @@ receive message payloads via FCM:
3737
On iOS, macOS, web and Android 13 (or newer), before FCM payloads can be
3838
received on your device, you must first ask the user's permission.
3939

40-
The `firebase_messaging` package provides a simple API for requesting permission via the [`requestPermission`](https://pub.dev/documentation/firebase_messaging/latest/firebase_messaging/FirebaseMessaging/requestPermission.html) method.
40+
The `firebase_messaging` package provides an API for requesting permission using the [`requestPermission`](https://pub.dev/documentation/firebase_messaging/latest/firebase_messaging/FirebaseMessaging/requestPermission.html) method.
4141
This API accepts a number of named arguments which define the type of permissions you'd like to request, such as whether
42-
messaging containing notification payloads can trigger a sound or read out messages via Siri. By default,
42+
messaging containing notification payloads can trigger a sound or read out messages using Siri. By default,
4343
the method requests sensible default permissions. The reference API provides full documentation on what each permission is for.
4444

45-
To get started, call the method from your application (on iOS a native modal will be displayed, on web
46-
the browser's native API flow will be triggered):
45+
To get started, call the method from your application (on iOS a built-in modal will be displayed, on web
46+
the browser's API flow will be triggered):
4747

4848
```dart
4949
FirebaseMessaging messaging = FirebaseMessaging.instance;
@@ -71,21 +71,21 @@ the request can be used to determine the user's overall decision:
7171

7272
Note: On Android versions prior to 13, `authorizationStatus` returns
7373
`authorized` if the user has not disabled notifications for the app in the
74-
operating system settings. On Android versions 13 and above, there is no way to determine if the user has chosen whether to grant/deny permission. A `denied` value conveys an undetermined or denied permission state, and it will be up to you to track if a permission request has been made.
74+
operating system settings. On Android versions 13 and higher, there is no way to determine if the user has chosen whether to grant/deny permission. A `denied` value conveys an undetermined or denied permission state, and it will be up to you to track if a permission request has been made.
7575

7676
The other properties on `NotificationSettings` return whether a specific permission is enabled, disabled or not supported on the current
7777
device.
7878

7979
Once permission has been granted and the different types of device state have been understood, your application can now start to handle the incoming
8080
FCM payloads.
8181

82-
## Message handling
82+
## Message handling {: #message-handling}
8383

8484
Based on your application's current state, incoming payloads of different
85-
[message types](/docs/cloud-messaging/concept-options#notifications_and_data_messages)
85+
[message types](/docs/cloud-messaging/customize-messages/set-message-type)
8686
require different implementations to handle them:
8787

88-
### Foreground messages
88+
### Foreground messages {: #foreground-messages}
8989

9090
To handle messages while your application is in the foreground, listen to the `onMessage` stream.
9191

@@ -105,20 +105,20 @@ various information about the payload, such as where it was from, the unique ID,
105105
a notification and more. Since the message was retrieved whilst your application is in the foreground, you can directly access your Flutter
106106
application's state and context.
107107

108-
#### Foreground and Notification messages
108+
#### Foreground and Notification messages {: #foreground-and-notification-messages}
109109

110-
Notification messages which arrive while the application is in the foreground will not display a visible notification by default, on both
110+
Notification messages which arrive while the application is in the foreground won't display a visible notification by default, on both
111111
Android and iOS. It is, however, possible to override this behavior:
112112

113113
- On Android, you must create a "High Priority" notification channel.
114114
- On iOS, you can update the presentation options for the application.
115115

116-
### Background messages
116+
### Background messages {: #background-messages}
117117

118-
The process of handling background messages is different on native (Android and
119-
Apple) and web based platforms.
118+
The process of handling background messages is different on Android,
119+
Apple, and web based platforms.
120120

121-
#### Apple platforms and Android
121+
#### Apple platforms and Android {: #apple-android-platforms}
122122

123123
Handle background messages by registering a `onBackgroundMessage` handler. When messages are received, an
124124
isolate is spawned (Android only, iOS/macOS does not require a separate isolate) allowing you to handle messages even when your application is not running.
@@ -152,15 +152,16 @@ application state or execute any UI impacting logic. You can, however, perform l
152152
It is also recommended to complete your logic as soon as possible. Running long, intensive tasks impacts device performance
153153
and may cause the OS to terminate the process. If tasks run for longer than 30 seconds, the device may automatically kill the process.
154154

155-
#### Web
155+
#### Web {:#web}
156+
{:#web}
156157

157158
On the Web, write a JavaScript [Service Worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) which runs in the background.
158159
Use the service worker to handle background messages.
159160

160161
To get started, create a new file in the your `web` directory, and call it `firebase-messaging-sw.js`:
161162

162163
```js title=web/firebase-messaging-sw.js
163-
// Please see this file for the latest firebase-js-sdk version:
164+
// See this file for the latest firebase-js-sdk version:
164165
// https://github.com/firebase/flutterfire/blob/main/packages/firebase_core/firebase_core_web/lib/src/firebase_sdk_version.dart
165166
importScripts("https://www.gstatic.com/firebasejs/10.7.0/firebase-app-compat.js");
166167
importScripts("https://www.gstatic.com/firebasejs/10.7.0/firebase-messaging-compat.js");
@@ -277,22 +278,22 @@ If you are still using the old templating system, you can register the worker by
277278
</body>
278279
```
279280
280-
Next restart your Flutter application. The worker will be registered and any background messages will be handled via this file.
281+
Next restart your Flutter application. The worker will be registered and any background messages will be handled using this file.
281282
282-
### Handling Interaction
283+
### Handling Interaction {: #handling-interaction}
283284
284285
Since notifications are a visible cue, it is common for users to interact with them (by pressing). The default behavior on both Android and iOS is to open the
285286
application. If the application is terminated it will be started; if it is in the background it will be brought to the foreground.
286287
287-
Depending on the content of a notification, you may wish to handle the user's interaction when the application opens. For example, if a new chat message is sent via
288+
Depending on the content of a notification, you might want to handle the user's interaction when the application opens. For example, if a new chat message is sent using
288289
a notification and the user presses it, you may want to open the specific conversation when the application opens.
289290
290291
The `firebase-messaging` package provides two ways to handle this interaction:
291292
292293
- `getInitialMessage()`: If the application is opened from a terminated state a `Future` containing a `RemoteMessage` will be returned. Once consumed, the `RemoteMessage` will be removed.
293294
- `onMessageOpenedApp`: A `Stream` which posts a `RemoteMessage` when the application is opened from a background state.
294295
295-
It is recommended that both scenarios are handled to ensure a smooth UX for your users. The code example below outlines how this can be achieved:
296+
It is recommended that both scenarios are handled to ensure a smooth UX for your users. The following code example outlines how this can be achieved:
296297
297298
```dart
298299
class Application extends StatefulWidget {
@@ -314,7 +315,7 @@ class _Application extends State<Application> {
314315
_handleMessage(initialMessage);
315316
}
316317
317-
// Also handle any interaction when the app is in the background via a
318+
// Also handle any interaction when the app is in the background using a
318319
// Stream listener
319320
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
320321
}
@@ -343,18 +344,18 @@ class _Application extends State<Application> {
343344
}
344345
```
345346
346-
How you handle interaction depends on your application setup. The above example shows a basic illustration using a StatefulWidget.
347+
How you handle interaction depends on your application setup. The previous example shows a basic illustration using a StatefulWidget.
347348
348-
## Localize Messages
349+
## Localize Messages {: #localize-messages}
349350
350351
You can send localized strings in two different ways:
351352
352353
- Store the preferred language of each of your users in your server and send customized notifications for each language
353-
- Embed localized strings in your app and make use of the operating system's native locale settings
354+
- Embed localized strings in your app and make use of the operating system's built-in locale settings
354355
355356
Here's how to use the second method:
356357
357-
### Android
358+
### Android {:#android}
358359
359360
1. Specify your default-language messages in `resources/values/strings.xml`:
360361
@@ -385,18 +386,18 @@ Here's how to use the second method:
385386
}
386387
```
387388
388-
### iOS
389+
### iOS {:#ios}
389390
390391
1. Specify your default-language messages in `Base.lproj/Localizable.strings`:
391392
392-
```
393+
```none
393394
"NOTIFICATION_TITLE" = "Hello World";
394395
"NOTIFICATION_MESSAGE" = "This is a message";
395396
```
396397
397398
2. Specify the translated messages in the <code><var>language</var>.lproj</code> directory. For example, specify French messages in `fr.lproj/Localizable.strings`:
398399
399-
```
400+
```none
400401
"NOTIFICATION_TITLE" = "Bonjour le monde";
401402
"NOTIFICATION_MESSAGE" = "C'est un message";
402403
```
@@ -416,25 +417,25 @@ Here's how to use the second method:
416417
}
417418
```
418419
419-
## Enable message delivery data export
420+
## Enable message delivery data export {: #enable-message-delivery}
420421
421-
You can export your message data into BigQuery for further analysis. BigQuery allows you to analyze the data using BigQuery SQL,
422+
You can export your message data into BigQuery for further analysis. BigQuery lets you analyze the data using BigQuery SQL,
422423
export it to another cloud provider, or use the data for your custom ML models. An export to BigQuery
423-
includes all available data for messages, regardless of message type or whether the message is sent via
424+
includes all available data for messages, regardless of message type or whether the message is sent using
424425
the API or the Notifications composer.
425426
426-
To enable the export, first follow the steps [described here](https://firebase.google.com/docs/cloud-messaging/understand-delivery?platform=ios#bigquery-data-export),
427+
To enable the export, first follow the steps in the [Understand delivery](https://firebase.google.com/docs/cloud-messaging/understand-delivery?platform=ios#bigquery-data-export) document,
427428
then follow these instructions:
428429
429-
### Android
430+
### Android {:#android-2}
430431
431432
You can use the following code:
432433
433434
```dart
434435
await FirebaseMessaging.instance.setDeliveryMetricsExportToBigQuery(true);
435436
```
436437
437-
### iOS
438+
### iOS {:#ios-2}
438439
439440
For iOS, you need to change the `AppDelegate.m` with the following content.
440441
@@ -461,10 +462,10 @@ For iOS, you need to change the `AppDelegate.m` with the following content.
461462
@end
462463
```
463464
464-
### Web
465+
### Web {:#web-2}
465466
466467
For Web, you need to change your service worker in order to use the v9 version of the SDK.
467-
The v9 version needs to be bundled, so you need to use a bundler like `esbuild` for instance
468+
The v9 version needs to be bundled, so you need to use a bundler like `esbuild`
468469
to get the service worker to work.
469470
See [the example app](https://github.com/firebase/flutterfire/blob/main/packages/firebase_messaging/firebase_messaging/example/bundled-service-worker) to see how to achieve this.
470471
@@ -483,22 +484,22 @@ experimentalSetDeliveryMetricsExportedToBigQueryEnabled(messaging, true);
483484
484485
Don't forget to run `yarn build` in order to export the new version of your service worker to the `web` folder.
485486
486-
## Display images in notifications on iOS
487+
## Display images in notifications on iOS {: #display-images}
487488
488489
On Apple devices, in order for incoming FCM Notifications to display images from the FCM payload, you must add an additional notification service extension and configure your app to use it.
489490
490491
If you are using Firebase phone authentication, you must add the Firebase Auth pod to your Podfile.
491492
492493
Note: The iOS simulator does not display images in push notifications. You must test on a physical device.
493494
494-
### Step 1 - Add a notification service extension
495+
### Step 1 - Add a notification service extension {:#step-1-add-notification-service-extension}
495496
496497
1. In Xcode, click **File > New > Target...**
497-
1. A modal will present a list of possible targets; scroll down or use the filter to select **Notification Service Extension**. Click **Next**.
498+
1. A modal will present a list of possible targets; scroll to or use the filter to select **Notification Service Extension**. Click **Next**.
498499
1. Add a product name (use "ImageNotification" to follow along with this tutorial), select either `Swift` or `Objective-C`, and click **Finish**.
499500
1. Enable the scheme by clicking **Activate**.
500501
501-
### Step 2 - Add target to the Podfile
502+
### Step 2 - Add target to the Podfile {:#step-2-add-target-podfile}
502503
503504
* {Swift}
504505
@@ -507,7 +508,7 @@ Note: The iOS simulator does not display images in push notifications. You must
507508
1. From the Navigator, [add the Firebase Apple platforms SDK](https://firebase.google.com/docs/ios/setup#add-sdks): **File > Add Package Dependencies...**
508509
509510
1. Search or enter package URL:
510-
```
511+
```none
511512
https://github.com/firebase/firebase-ios-sdk
512513
```
513514
@@ -521,7 +522,7 @@ Note: The iOS simulator does not display images in push notifications. You must
521522
522523
1. From the Navigator, open the Podfile: **Pods > Podfile**
523524
524-
1. Scroll down to the bottom of the file and add:
525+
1. Go to the bottom of the file and add:
525526
526527
```ruby
527528
target 'ImageNotification' do
@@ -533,7 +534,7 @@ Note: The iOS simulator does not display images in push notifications. You must
533534
534535
1. Install or update your pods using `pod install` from the `ios` or `macos` directory.
535536
536-
### Step 3 - Use the extension helper
537+
### Step 3 - Use the extension helper {:#step-3-ext-helper}
537538
538539
At this point, everything should still be running normally. The final step is invoking the extension helper.
539540
@@ -575,7 +576,7 @@ At this point, everything should still be running normally. The final step is in
575576
576577
1. Open the `NotificationService.m` file.
577578
578-
1. At the top of the file, import `FirebaseMessaging.h` right after the `NotificationService.h` as shown below.
579+
1. At the top of the file, import `FirebaseMessaging.h` right after the `NotificationService.h`.
579580
580581
Replace the content of `NotificationService.m` with:
581582
@@ -628,6 +629,6 @@ At this point, everything should still be running normally. The final step is in
628629
@end
629630
```
630631
631-
### Step 4 - Add the image to the payload
632+
### Step 4 - Add the image to the payload {:#add-image-payload}
632633
633634
In your notification payload, you can now add an image. See the iOS documentation on [how to build a send request](https://firebase.google.com/docs/cloud-messaging/ios/send-image#build_the_send_request). Keep in mind that a 300KB max image size is enforced by the device.

0 commit comments

Comments
 (0)