You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cloud-messaging/receive.md
+47-46Lines changed: 47 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ is first important to establish the various states a device can be in:
25
25
| **Terminated** | When the device is locked or the application is not running.
26
26
27
27
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:
29
29
30
30
- The application must have opened at least once (to allow for registration with FCM).
31
31
- 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:
37
37
On iOS, macOS, web and Android 13 (or newer), before FCM payloads can be
38
38
received on your device, you must first ask the user's permission.
39
39
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.
41
41
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,
43
43
the method requests sensible default permissions. The reference API provides full documentation on what each permission is for.
44
44
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
@@ -71,21 +71,21 @@ the request can be used to determine the user's overall decision:
71
71
72
72
Note: On Android versions prior to 13, `authorizationStatus` returns
73
73
`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.
75
75
76
76
The other properties on `NotificationSettings` return whether a specific permission is enabled, disabled or not supported on the current
77
77
device.
78
78
79
79
Once permission has been granted and the different types of device state have been understood, your application can now start to handle the incoming
80
80
FCM payloads.
81
81
82
-
## Message handling
82
+
## Message handling {: #message-handling}
83
83
84
84
Based on your application's current state, incoming payloads of different
To handle messages while your application is in the foreground, listen to the `onMessage` stream.
91
91
@@ -105,20 +105,20 @@ various information about the payload, such as where it was from, the unique ID,
105
105
a notification and more. Since the message was retrieved whilst your application is in the foreground, you can directly access your Flutter
106
106
application's state and context.
107
107
108
-
#### Foreground and Notification messages
108
+
#### Foreground and Notification messages {: #foreground-and-notification-messages}
109
109
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
111
111
Android and iOS. It is, however, possible to override this behavior:
112
112
113
113
- On Android, you must create a "High Priority" notification channel.
114
114
- On iOS, you can update the presentation options for the application.
115
115
116
-
### Background messages
116
+
### Background messages {: #background-messages}
117
117
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.
120
120
121
-
#### Apple platforms and Android
121
+
#### Apple platforms and Android {: #apple-android-platforms}
122
122
123
123
Handle background messages by registering a `onBackgroundMessage` handler. When messages are received, an
124
124
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
152
152
It is also recommended to complete your logic as soon as possible. Running long, intensive tasks impacts device performance
153
153
and may cause the OS to terminate the process. If tasks run for longer than 30 seconds, the device may automatically kill the process.
154
154
155
-
#### Web
155
+
#### Web {:#web}
156
+
{:#web}
156
157
157
158
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.
158
159
Use the service worker to handle background messages.
159
160
160
161
To get started, create a new file in the your `web` directory, and call it `firebase-messaging-sw.js`:
161
162
162
163
```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:
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
285
286
application. If the application is terminated it will be started; if it is in the background it will be brought to the foreground.
286
287
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
288
289
a notification and the user presses it, you may want to open the specific conversation when the application opens.
289
290
290
291
The `firebase-messaging` package provides two ways to handle this interaction:
291
292
292
293
- `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.
293
294
- `onMessageOpenedApp`: A `Stream` which posts a `RemoteMessage` when the application is opened from a background state.
294
295
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:
296
297
297
298
```dart
298
299
classApplicationextendsStatefulWidget {
@@ -314,7 +315,7 @@ class _Application extends State<Application> {
314
315
_handleMessage(initialMessage);
315
316
}
316
317
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
@@ -343,18 +344,18 @@ class _Application extends State<Application> {
343
344
}
344
345
```
345
346
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.
347
348
348
-
## Localize Messages
349
+
## Localize Messages {: #localize-messages}
349
350
350
351
You can send localized strings in two different ways:
351
352
352
353
- 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
354
355
355
356
Here's how to use the second method:
356
357
357
-
### Android
358
+
### Android {:#android}
358
359
359
360
1. Specify your default-language messages in `resources/values/strings.xml`:
360
361
@@ -385,18 +386,18 @@ Here's how to use the second method:
385
386
}
386
387
```
387
388
388
-
### iOS
389
+
### iOS {:#ios}
389
390
390
391
1. Specify your default-language messages in `Base.lproj/Localizable.strings`:
391
392
392
-
```
393
+
```none
393
394
"NOTIFICATION_TITLE" = "Hello World";
394
395
"NOTIFICATION_MESSAGE" = "This is a message";
395
396
```
396
397
397
398
2. Specify the translated messages in the <code><var>language</var>.lproj</code> directory. For example, specify French messages in `fr.lproj/Localizable.strings`:
398
399
399
-
```
400
+
```none
400
401
"NOTIFICATION_TITLE" = "Bonjour le monde";
401
402
"NOTIFICATION_MESSAGE" = "C'est un message";
402
403
```
@@ -416,25 +417,25 @@ Here's how to use the second method:
416
417
}
417
418
```
418
419
419
-
## Enable message delivery data export
420
+
## Enable message delivery data export {: #enable-message-delivery}
420
421
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,
422
423
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
424
425
the API or the Notifications composer.
425
426
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,
For iOS, you need to change the `AppDelegate.m` with the following content.
440
441
@@ -461,10 +462,10 @@ For iOS, you need to change the `AppDelegate.m` with the following content.
461
462
@end
462
463
```
463
464
464
-
### Web
465
+
### Web {:#web-2}
465
466
466
467
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`
468
469
to get the service worker to work.
469
470
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.
Don't forget to run `yarn build` in order to export the new version of your service worker to the `web` folder.
485
486
486
-
## Display images in notifications on iOS
487
+
## Display images in notifications on iOS {: #display-images}
487
488
488
489
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.
489
490
490
491
If you are using Firebase phone authentication, you must add the Firebase Auth pod to your Podfile.
491
492
492
493
Note: The iOS simulator does not display images in push notifications. You must test on a physical device.
493
494
494
-
### Step 1 - Add a notification service extension
495
+
### Step 1 - Add a notification service extension {:#step-1-add-notification-service-extension}
495
496
496
497
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**.
498
499
1. Add a product name (use "ImageNotification" to follow along with this tutorial), select either `Swift` or `Objective-C`, and click **Finish**.
499
500
1. Enable the scheme by clicking **Activate**.
500
501
501
-
### Step 2 - Add target to the Podfile
502
+
### Step 2 - Add target to the Podfile {:#step-2-add-target-podfile}
502
503
503
504
* {Swift}
504
505
@@ -507,7 +508,7 @@ Note: The iOS simulator does not display images in push notifications. You must
507
508
1. From the Navigator, [add the Firebase Apple platforms SDK](https://firebase.google.com/docs/ios/setup#add-sdks): **File > Add Package Dependencies...**
508
509
509
510
1. Search or enter package URL:
510
-
```
511
+
```none
511
512
https://github.com/firebase/firebase-ios-sdk
512
513
```
513
514
@@ -521,7 +522,7 @@ Note: The iOS simulator does not display images in push notifications. You must
521
522
522
523
1. From the Navigator, open the Podfile: **Pods > Podfile**
523
524
524
-
1. Scroll down to the bottom of the file and add:
525
+
1. Go to the bottom of the file and add:
525
526
526
527
```ruby
527
528
target 'ImageNotification' do
@@ -533,7 +534,7 @@ Note: The iOS simulator does not display images in push notifications. You must
533
534
534
535
1. Install or update your pods using `pod install` from the `ios` or `macos` directory.
535
536
536
-
### Step 3 - Use the extension helper
537
+
### Step 3 - Use the extension helper {:#step-3-ext-helper}
537
538
538
539
At this point, everything should still be running normally. The final step is invoking the extension helper.
539
540
@@ -575,7 +576,7 @@ At this point, everything should still be running normally. The final step is in
575
576
576
577
1. Open the `NotificationService.m` file.
577
578
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`.
579
580
580
581
Replace the content of `NotificationService.m` with:
581
582
@@ -628,6 +629,6 @@ At this point, everything should still be running normally. The final step is in
628
629
@end
629
630
```
630
631
631
-
### Step 4 - Add the image to the payload
632
+
### Step 4 - Add the image to the payload {:#add-image-payload}
632
633
633
634
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