Skip to content

Commit ee5e283

Browse files
authored
docs(firebase pn): Update Push Notifications with Firebase Guide (#2698)
- References to Ionic 5 instead of Ionic 4. - Prompts to install the '@ionic/cli' instead of previous 'ionic' cli package. - Reminds user to build and copy web assets to capacitor after writing code. - Adds instructions on Android app creation
1 parent 04fb275 commit ee5e283

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

site/docs-md/guides/push-notifications-firebase.md

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ description: Learn how to get Firebase Cloud Messaging working on iOS and Androi
44
url: /docs/guides/push-notifications-firebase
55
contributors:
66
- bryplano
7+
- javebratt
78
---
89

910
# Using Push Notifications with Firebase in an Ionic + Angular App
1011

11-
**Web Framework**: Ionic 4 + Angular
12+
**Web Framework**: Angular
1213
**Platforms**: iOS, Android
1314

1415
One of the most common features provided by application developers to their users is push notifications. In this tutorial, we'll walk through all the steps needed to get [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) working on iOS and Android.
@@ -21,16 +22,21 @@ Building and deploying iOS and Android applications using Capacitor requires a b
2122

2223
To test push notifications on iOS, Apple requires that you have [a paid Apple Developer account](https://developer.apple.com/) and a *physical* iOS device.
2324

25+
If you are running into issues or your console throws warnings about outdated or deprecated packages, make sure that you're on the latest stable versions of Node, Android Studio, and Xcode.
26+
27+
Also, we're using Firebase for push notifications, so if you're using other Cordova plugins that use the Firebase SDK make sure they're using the latest versions.
28+
2429
## Prepare an Ionic Capacitor App
25-
If you have an existing Ionic 4 app, skip this section. If not, let's create an Ionic app first.
30+
31+
If you have an existing Ionic app, skip this section. If not, let's create an Ionic app first.
2632

2733
In your preferred terminal, install the latest version of the Ionic CLI:
2834

2935
```bash
30-
npm install -g ionic
36+
npm install -g @ionic/cli
3137
```
3238

33-
Next, let's use the CLI to create a new Ionic 4 Angular app based on the **blank** starter project and call it **capApp**:
39+
Next, let's use the CLI to create a new Ionic Angular app based on the **blank** starter project and call it **capApp**:
3440

3541
```bash
3642
ionic start capApp blank --type=angular
@@ -105,28 +111,28 @@ ngOnInit() {
105111
});
106112

107113
// On success, we should be able to receive notifications
108-
PushNotifications.addListener('registration',
114+
PushNotifications.addListener('registration',
109115
(token: PushNotificationToken) => {
110116
alert('Push registration success, token: ' + token.value);
111117
}
112118
);
113119

114120
// Some issue with our setup and push will not work
115-
PushNotifications.addListener('registrationError',
121+
PushNotifications.addListener('registrationError',
116122
(error: any) => {
117123
alert('Error on registration: ' + JSON.stringify(error));
118124
}
119125
);
120126

121127
// Show us the notification payload if the app is open on our device
122-
PushNotifications.addListener('pushNotificationReceived',
128+
PushNotifications.addListener('pushNotificationReceived',
123129
(notification: PushNotification) => {
124130
alert('Push received: ' + JSON.stringify(notification));
125131
}
126132
);
127133

128134
// Method called when tapping on a notification
129-
PushNotifications.addListener('pushNotificationActionPerformed',
135+
PushNotifications.addListener('pushNotificationActionPerformed',
130136
(notification: PushNotificationActionPerformed) => {
131137
alert('Push action performed: ' + JSON.stringify(notification));
132138
}
@@ -170,25 +176,25 @@ export class HomePage implements OnInit {
170176
}
171177
});
172178

173-
PushNotifications.addListener('registration',
179+
PushNotifications.addListener('registration',
174180
(token: PushNotificationToken) => {
175181
alert('Push registration success, token: ' + token.value);
176182
}
177183
);
178184

179-
PushNotifications.addListener('registrationError',
185+
PushNotifications.addListener('registrationError',
180186
(error: any) => {
181187
alert('Error on registration: ' + JSON.stringify(error));
182188
}
183189
);
184190

185-
PushNotifications.addListener('pushNotificationReceived',
191+
PushNotifications.addListener('pushNotificationReceived',
186192
(notification: PushNotification) => {
187193
alert('Push received: ' + JSON.stringify(notification));
188194
}
189195
);
190196

191-
PushNotifications.addListener('pushNotificationActionPerformed',
197+
PushNotifications.addListener('pushNotificationActionPerformed',
192198
(notification: PushNotificationActionPerformed) => {
193199
alert('Push action performed: ' + JSON.stringify(notification));
194200
}
@@ -197,6 +203,13 @@ export class HomePage implements OnInit {
197203
}
198204
```
199205
206+
After this, you'll want to generate a new build and let Capacitor know about the changes. You can do that with:
207+
208+
```bash
209+
ionic build
210+
npx cap copy
211+
```
212+
200213
## Creating a Project for your App on Firebase
201214
202215
Before we can connect Firebase Cloud Messaging to your application and send push notifications, you'll need to start a project in Firebase.
@@ -211,7 +224,9 @@ Name the project, accept the Firebase ToS and click **Create project** to contin
211224
212225
This section more-or-less mirrors the [setting up Firebase using the Firebase console documentation](https://firebase.google.com/docs/android/setup?authuser=0). See below for specific Capacitor-related notes.
213226
214-
Go to the Project Overview page for your Firebase project and, under the **Grow** section, click the **Cloud Messaging** option. At the top, click on the **Android** icon.
227+
Go to the Project Overview page for your Firebase project and at the top, click on the **Android** icon to add a new android application.
228+
229+
![Add new Android Application in Firebase Console](/assets/img/docs/guides/firebase-push-notifications/add-android-app.png)
215230
216231
The next screen will ask you for some information about your application.
217232
@@ -270,7 +285,7 @@ You'll then want to open Xcode...
270285
npx cap open ios
271286
```
272287
273-
... and move the `.plist` file into your Xcode project as instructed by Firebase, ensuring to add it to all targets.
288+
... and move the `.plist` file into your Xcode project as instructed by Firebase, ensuring to add it to all targets.
274289
275290
![Google Service Info Plist Location for iOS](/assets/img/docs/guides/firebase-push-notifications/google-plist-location-ios.png)
276291
@@ -307,7 +322,7 @@ def capacitor_pods
307322
# Automatic Capacitor Pod dependencies, do not delete
308323
pod 'Capacitor', :path => '../../node_modules/@capacitor/ios'
309324
pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios'
310-
325+
311326
# Do not delete
312327
end
313328

@@ -363,7 +378,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
363378
return true
364379
}
365380
```
381+
366382
If you would like to recieve the firebase FCM token from iOS instead of the raw APNS token, you will need to also change your `AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken` code to look like this:
383+
367384
```swift
368385
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
369386
Messaging.messaging().apnsToken = deviceToken
@@ -377,7 +394,6 @@ If you would like to recieve the firebase FCM token from iOS instead of the raw
377394
}
378395
```
379396

380-
381397
### Upload the APNS Certificate or Key to Firebase
382398

383399
If you followed the instructions from the beginning, you'll have created an Apple APNS Certificate or an APNS Auth Key in the Apple Developer portal. You need to upload one of these to Firebase before Firebase can talk to APNS and send push notifications to your application.
@@ -395,11 +411,13 @@ Now for the fun part - let's verify that push notifications from Firebase are wo
395411
We need to fire up our application on Android or iOS so that our `home.page.ts` page can register and receive notifications.
396412

397413
To open your Android project in Android Studio:
414+
398415
```bash
399416
npx cap open android
400417
```
401418

402419
To open your iOS project in Xcode:
420+
403421
```bash
404422
npx cap open ios
405423
```
@@ -410,9 +428,9 @@ Once the project is open, side-load the application on your device using the Run
410428

411429
If your app successfully registers and you followed the code above, you should see an alert with a success message!
412430

413-
Now we'll test to see if the notifications are received by our device. To send a notification, in Firebase, go to the **Cloud Messaging** section under the Grow header in the project pane.
431+
Now we'll test to see if the notifications are received by our device. To send a notification, in Firebase, go to the **Cloud Messaging** section under the Grow header in the project pane.
414432

415-
Next, select the **New Notification** button.
433+
Next, select the **New Notification** button.
416434

417435
When creating the notification, you only need to specify the following information:
418436

175 KB
Loading

0 commit comments

Comments
 (0)