|
| 1 | +--- |
| 2 | +title: Push Notifications |
| 3 | +slug: /concepts/alerts-notification/push-notifications |
| 4 | +sidebar_position: 2 |
| 5 | +tags: [Actions, Alerts & Notifications, Integration] |
| 6 | +keywords: [FlutterFlow, Actions, Alerts & Notifications, Push Notifications] |
| 7 | +--- |
| 8 | + |
| 9 | + |
| 10 | +# Push Notifications |
| 11 | + |
| 12 | +Push notifications allow you to send timely and relevant information to a user's phone. From this page, you can enable the push notification feature in your app. After enabling it, you can send the push notification instantly or schedule it to be delivered to the user's device at a specific time. |
| 13 | + |
| 14 | +:::info[Behind the scenes] |
| 15 | +FlutterFlow uses [**Firebase Cloud Messaging**](https://firebase.google.com/docs/cloud-messaging) (A Firebase product) to send push notifications. |
| 16 | +::: |
| 17 | + |
| 18 | + |
| 19 | +:::note[General Prerequisites] |
| 20 | + |
| 21 | +Before getting started with this section, ensure you have: |
| 22 | + |
| 23 | +* Completed all steps in the |
| 24 | +[**Firebase Setup**](../../ff-integrations/firebase/connect-to-firebase-setup.md). |
| 25 | +* Upgraded to a [**Blaze plan**](https://firebase.google.com/pricing) for your Firebase project. |
| 26 | +::: |
| 27 | + |
| 28 | +:::note[iOS Prerequisites] |
| 29 | + |
| 30 | +These steps are mandatory if you want to send push notifications to iOS devices: |
| 31 | + |
| 32 | +* Created an [**Apple account**](https://appleid.apple.com/account?appId=632&returnUrl=https%3A//developer.apple.com/account/). |
| 33 | +* [**Purchased an Apple Developer membership**](https://developer.apple.com/programs/enroll/). Please see [this link](https://developer.apple.com/programs/) for more details on the Apple Developer program and how to sign up. |
| 34 | + |
| 35 | +::: |
| 36 | + |
| 37 | +## Add push notifications |
| 38 | + |
| 39 | +Here are the steps to send push notifications: |
| 40 | + |
| 41 | +1. [Enabling push notification](#enabling-push-notification) |
| 42 | +2. [Only for iOS: Configuring iOS app](#only-for-ios-configuring-ios-app) |
| 43 | +3. [Sending push notifications](#3-sending-push-notifications) |
| 44 | + |
| 45 | +### Enabling push notification |
| 46 | + |
| 47 | +:::warning |
| 48 | +**Please note, push notifications will not work in these scenarios:** |
| 49 | + |
| 50 | +* Push notifications will not work on an iOS simulator. To test you will need to use a real device. |
| 51 | +* Push notifications will not be delivered to users who are logged out of your app. To send push notifications to users who are not logged in, consider implementing [**Anonymous Firebase Login**](../../ff-integrations/authentication/firebase-auth/anonymous-login.md) within your app |
| 52 | +* Push notifications will not work if you have the app open on your device. |
| 53 | +::: |
| 54 | + |
| 55 | +To enable push notifications: |
| 56 | + |
| 57 | +1. Click on the **Settings and Integrations** -> App Settings -> **Push Notifications** and |
| 58 | +**Enable Push Notifications**. |
| 59 | +2. Now, click on the **Deploy** button. This will create and deploy the *Cloud Functions* in your |
| 60 | + Firebase project that are necessary for push notifications to work. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +### Only for iOS: Configuring iOS app |
| 65 | + |
| 66 | +To receive the push notifications in an iOS app, you need to perform the following additional steps. |
| 67 | + |
| 68 | +#### 1. Creating a key |
| 69 | + |
| 70 | +Apple requires developers to create a key for the push notifications inside the *Apple Developer Console* to verify the push notification's sender. |
| 71 | + |
| 72 | +To create a key: |
| 73 | + |
| 74 | +1. From your Apple developer account, open the [**keys**](https://developer.apple.com/account/resources/authkeys/list) section. |
| 75 | +2. Click on the **(+)** button on the right side of the **Keys** label. |
| 76 | +3. Enter the **Key Name**. |
| 77 | +4. Select the **Apple Push Notifications service (APNs)** from the list below. |
| 78 | +5. Click on **Continue** and then **Register**. |
| 79 | +6. Click **Download** and save the file. It will be needed to add to your Firebase project. |
| 80 | + |
| 81 | +<div class="video-container"><iframe src="https://www.loom. |
| 82 | +com/embed/d17a807b1cc44f6caaa9b606f70819ed?sid=c983c35a-66d3-460f-8898-3329581d2801" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +#### 2. Add APNs key to Firebase project |
| 87 | + |
| 88 | +To add APNs key to Firebase project: |
| 89 | + |
| 90 | +1. From the Firebase dashboard from your project, navigate to the far left menu. Select Project |
| 91 | + Settings( ) --> Project Settings. |
| 92 | + |
| 93 | +2. Select the **Cloud Messaging** tab. |
| 94 | + |
| 95 | +3. Scroll down and find your iOS app under the **Apple app configuration** section. |
| 96 | + |
| 97 | +4. Under the **APNs Authentication Key** section, click on **Upload**. A popup will appear to |
| 98 | + upload and enter the key details. |
| 99 | + |
| 100 | +5. Click on the **Browse** button to locate and upload the APNs auth key file. |
| 101 | + |
| 102 | +6. Enter the *Key ID*. You can find your Key ID in the Apple Developer Center under [Certificates, |
| 103 | + Identifiers & Profiles](https://developer.apple.com/account/resources) -> [Keys](https://developer.apple.com/account/resources/authkeys/list) -> Click on **Key**. |
| 104 | + |
| 105 | +7. Enter the *Team ID*. You can find your Team ID in the Apple Member Center under the [membership |
| 106 | + tab](https://developer.apple.com/account/#/membership). |
| 107 | + |
| 108 | +<div class="video-container"><iframe src="https://www.loom. |
| 109 | +com/embed/03c8dd166901446cb8667e60acadb0de?sid=d63319fe-6c4f-4219-9448-97aeb1777c7d" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +#### 3. Adding identifier |
| 114 | + |
| 115 | +You must add an Identifier to be able to send the push notifications to the iOS devices after you deploy your app to the app store. |
| 116 | + |
| 117 | +To add an Identifier: |
| 118 | + |
| 119 | +1. From your *Apple developer account* open the [**Identifiers**](https://developer.apple.com/account/resources/identifiers/list) section. |
| 120 | + |
| 121 | +2. Click on the **(+)** button on the right side of the **Identifiers** label. |
| 122 | + |
| 123 | +3. Select the **App IDs** and click **Continue**. |
| 124 | + |
| 125 | +4. Select the *Type* **App** and click **Continue.** |
| 126 | + |
| 127 | +5. Enter the **Description** as **ImageNotification.** |
| 128 | + |
| 129 | +6. Enter the **Bundle ID** as your package name and add **.ImageNotification** in the end. |
| 130 | + |
| 131 | +7. Click **Continue** and then click **Register**. |
| 132 | + |
| 133 | +<div class="video-container"><iframe src="https://www.loom. |
| 134 | +com/embed/5a93753eab644628a4bf67a35cf0eea8?sid=01d57898-deec-4ba6-af29-7d87f1b4ab30" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +#### 4. Enable push notifications in your App ID |
| 139 | + |
| 140 | +:::note |
| 141 | +Continue with this step only if you have an APP or Bundle ID created in your developer account. |
| 142 | +::: |
| 143 | + |
| 144 | +Add the 'Push Notifications' capability to your existing App ID in order to avoid any issues when you re-deploy your app. Here's how you do it: |
| 145 | + |
| 146 | +1. From your *Apple developer account,* open the [**Identifiers**](https://developer.apple.com/account/resources/identifiers/list) section. |
| 147 | + |
| 148 | +2. Open the identifier with your existing APP ID. |
| 149 | + |
| 150 | +3. Select **Push Notifications** from the list and click **Save**. |
| 151 | + |
| 152 | +<div class="video-container"><iframe src="https://www.loom. |
| 153 | +com/embed/06916f7d74d1418d9fdb818d1e001635?sid=bb4c6e0e-ea37-48d7-b52f-d4a81db15dc9" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +### Sending push notifications |
| 158 | + |
| 159 | +Use these steps to create and send push notifications: |
| 160 | + |
| 161 | +1. Click on the **Settings and Integrations** -> App Settings -> **Push Notifications**. |
| 162 | + |
| 163 | +2. Find the **Manually Trigger Notifications** section and click on it to expand the section. |
| 164 | + |
| 165 | +3. In the **Notification Title**, enter the text that you want to set as a title for notification. |
| 166 | + |
| 167 | +4. In the **Notification Text**, enter the content of your notification. |
| 168 | +5. Optional: Click on the **Upload Notification Image** to set the image that you want to appear in the notification. |
| 169 | +6. Find the **Target Audience** dropdown. Keep the **All** option selected if you want to send notifications to both *Android* and *iOS* users. You can select **Android** or **iOS** if you wish to target OS-specific users. |
| 170 | +7. Enable **Deliver With Sound** if you wish to. |
| 171 | +8. If you have more than 10K users, you might want to enable the **Batch Notifications**. |
| 172 | +9. To send push notifications to a specific or few users, enter the user document reference (from the 'users' collection in Firestore) into the *User References*. |
| 173 | +10. Find the **Initial Page** dropdown and select a page that you would like to open when a user clicks on the notification. |
| 174 | +11. Click on **Send Notification**. |
| 175 | +12. To confirm you want to send this notification, a popup will appear. You need to enter the text '**Send Notification**' and click on **Send Notification** again. |
| 176 | + |
| 177 | +<div class="video-container"><iframe src="https://www.loom. |
| 178 | +com/embed/e60604c910ca45f58e995407c16110e9?sid=2c8fa30b-3b43-42fe-a12f-e09f82868953" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +## Push notifications with data |
| 183 | + |
| 184 | +Sometimes you might want to include additional data with your push notifications, which can then be used to display more detailed information on the page when it's opened through a push notification. |
| 185 | + |
| 186 | +For instance, consider a news app that sends push notifications for breaking news. When the user taps the notification, the additional data could include the news article's title, summary, and image. |
| 187 | + |
| 188 | +:::warning[Note] |
| 189 | +Currently we only support sending *Firestore document references* as data. |
| 190 | +::: |
| 191 | + |
| 192 | +To send a push notification with data: |
| 193 | + |
| 194 | +1. You must have a page that accepts a parameter of type 'DocumentReference'. |
| 195 | +2. Build a notification using the instructions [here](#add-push-notifications). |
| 196 | +3. Set the **Initial Page** to the one that accepts the parameter. |
| 197 | +4. Find the **Parameter Data** section, copy the document reference from the Firestore, and paste it into the input box below. |
| 198 | +5. Click on **Send Notification**. |
| 199 | + |
| 200 | +<div class="video-container"><iframe src="https://www.loom. |
| 201 | +com/embed/f6a0aa69b9274b1c965a85d72dcfdda5?sid=dfdebe15-61bf-4bf1-8cd6-238c7b9a5744" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | +## Trigger push notification |
| 206 | + |
| 207 | +You might want to send a push notification when something happens in your app. For example, sending a push notification on a message sent, a push notification on a new appointment booked, a push notification on a price change, etc. |
| 208 | + |
| 209 | +You can send the push notification on such event occurrence by adding the **Trigger Push Notification** action. |
| 210 | + |
| 211 | +For demonstration purposes, consider an example of sending a push notification to a post creator when someone comments on a post. |
| 212 | + |
| 213 | +<div class="video-container"><iframe src="https://www.loom. |
| 214 | +com/embed/4e4e92b55f6948b58db2dd9dbb0929d4?sid=99960bc1-143e-4cb6-918b-75f0eecf29b4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +Here are the steps: |
| 219 | + |
| 220 | +### 1. Retrieve the document reference of the user |
| 221 | + |
| 222 | +The document reference of the user is required to send a push notification to a specific user, in this case, it would be the document reference of the post creator. |
| 223 | + |
| 224 | +Normally, you would obtain the user document reference from the Firestore document, where a field like 'created_by' stores the document reference of the post creator. However, for the sake of simplicity, let's assume that we are storing this reference in an app state variable, which looks like this: |
| 225 | + |
| 226 | +<figure> |
| 227 | +  |
| 228 | + <figcaption class="centered-caption">User document reference in app state variable</figcaption> |
| 229 | +</figure> |
| 230 | + |
| 231 | +If you need to send push notifications to multiple users, consider a scenario where you want to notify all users who liked a post when someone comments on it. You can retrieve the list or array of user document references from the Firestore document or any app state variable within your app. Here's what a list of document references in an app state variable might look like: |
| 232 | + |
| 233 | +<figure> |
| 234 | +  |
| 235 | + <figcaption class="centered-caption">Multiple user document references in app state variable</figcaption> |
| 236 | +</figure> |
| 237 | + |
| 238 | +:::info |
| 239 | +Learn more about working with |
| 240 | +[**App State variables**](../../resources/data-representation/app-state.md). |
| 241 | +::: |
| 242 | + |
| 243 | +### 2. Trigger Push Notification [Action] |
| 244 | + |
| 245 | +Using this action, you can trigger the push notification when an event occurs in your app. For example, sending the push notification to the post creator when someone comments on a post. |
| 246 | + |
| 247 | +Follow the steps below to add this action to any widget. |
| 248 | + |
| 249 | +1. Select the **Widget** (e.g., IconButton, Button, etc.) on which you want to add the action. |
| 250 | + |
| 251 | +2. Select **Actions** from the Properties Panel (the right menu), and click **Open**. This will open an **Action Flow Editor** in a new popup window. |
| 252 | + 1. Click on the **+ Add Action**. |
| 253 | + |
| 254 | + 2. On the right side, search and select the **Trigger Push Notification** action. |
| 255 | + 3. To set who should receive the push notification, set the **Audience** to **Single |
| 256 | + Recipient** or **Multiple Recipient**. |
| 257 | + * If you want to send a notification to a single user, set it to 'Single Recipient'. For example, sending a notification only to the group creator when someone joins the group. |
| 258 | + * If you want to send a notification to multiple users, set it to 'Multiple Recipients'. For example, sending a notification to all group members when someone joins the group. |
| 259 | + 4. Set the **Source** of user document reference (e.g., app state variable or Firestore document). |
| 260 | + 5. Further options are available based on the selected source. |
| 261 | + 6. To set the specific notification title text, enter the **Notification Title**. |
| 262 | + 7. You can set the **Notification Text** from a variable. |
| 263 | + 8. Optional: Open the **Notification Image** section to set the image that you want to appear in the notification. |
| 264 | + 9. Turn on the **Deliver with Sound** if you want to send a notification with the user's default sound/vibration. |
| 265 | + 10. To set which page to open when a user taps the notification, set the **Initial Page** to the page you would like to open. You can also [pass on the parameters](#push-notifications-with-data) if page requests. |
| 266 | + |
| 267 | +<div class="video-container"><iframe src="https://www.loom. |
| 268 | +com/embed/5a6fa52d6802486fa0581f5ced56899e?sid=ca075c66-1b9a-4938-b882-0b3d7c803cb5" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></div> |
| 269 | + |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | +--- |
| 274 | + |
| 275 | +## FAQs |
| 276 | + |
| 277 | +<details> |
| 278 | +<summary>Push notifications not working; Getting cloud function error: PERMISSION_DENIED: Missing or insufficient permissions</summary> |
| 279 | + |
| 280 | +If you encounter an error with push notifications, specifically a cloud function failure due to permission issues, it might be related to your Google Cloud organization's settings. Organizations can disable automatic IAM grants for default service accounts, leading to this error. |
| 281 | + |
| 282 | +To fix this issue, manually grant the Editor role to the default service account used by your project. You can do this by visiting the GCP IAM page and assigning the Editor role to the following service account: |
| 283 | + |
| 284 | +* For App Engine (Gen 1): `{project-id}@appspot.gserviceaccount.com` |
| 285 | +* For Compute Engine (Gen 2): `{project-number}[email protected]` |
| 286 | +</details> |
| 287 | + |
| 288 | + |
| 289 | + |
0 commit comments