Skip to content
This repository was archived by the owner on Feb 7, 2019. It is now read-only.

Commit 4db2219

Browse files
committed
chore: update migration steps
1 parent b3d0656 commit 4db2219

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

MIGRATE-TO-FIREBASE.md

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
This is a migration guide which can help you switch your app from using push-plugin to using nativescript-plugin-firebase
2-
1. Go to your app's root folder and execute
1+
# Migration guide push-plugin -> Firebase plugin
2+
If you have an app that uses push-plugin for push notifications and need to switch to nativescript-plugin-firebase, this guide can help you. If you are just starting with push notifications, however, it would be best to use [Firebase plugin]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase") and refer [its documentation on messaging]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md").
3+
#### 1. Add the plugin to your app
4+
Go to your app's root folder in terminal app and execute
35
```bash
46
tns plugin add nativescript-plugin-firebase
57
```
68
> Upon plugin installation, you'll be prompted to choose which features to use. Choose "yes" for Firebase Messaging (of course :)). By default, the plugin saves the configuration as a file (firebase.nativescript.json) to use it when reinstalling the plugin.
79
8-
2. Add `GoogleService-Info.plist` (for iOS) or `google-services.json` (for Android) in App_Resources/iOS (and App_Resources/Android, respectively). These are the configuration files that come from your Firebase apps. If you don't have such yet, go to https://console.firebase.google.com and create one. See [firebase plugin's docs]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md") for more info on initial setup.
10+
#### 2. Setup
11+
Add `GoogleService-Info.plist` (for iOS) or `google-services.json` (for Android) in App_Resources/iOS (and App_Resources/Android, respectively). These are the configuration files that come from your Firebase apps. If you don't have such yet, go to https://console.firebase.google.com and create one. See [Firebase plugin's docs]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md") for more info on initial setup.
912

10-
3. Add some code [to handle a notification]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#handling-a-notification")
13+
#### 3. Initialization prerequisite
14+
Make sure you [`require` the plugin in `app.ts` / `main.ts` / `main.aot.ts`](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/55cfb4f69cf8939f9101712fed22383196b08d36/demo/app/app.ts#L5)
15+
*before* `application.start()`, and do `init()` *after* the app has started (not in `app.ts` - not even in a timeout; move it out of `app.ts` entirely!). This ensures the notifications will be receivable in the background.
16+
17+
EXAMPLE:
18+
```js
19+
// in app.ts
20+
// ...
21+
const firebase = require("nativescript-plugin-firebase");
22+
// ...
23+
app.start({ moduleName: 'main-page' });
24+
```
25+
26+
#### 4. Add some code [to handle a notification]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#handling-a-notification")
1127

1228
EXAMPLE: The following code using push-plugin:
1329
```js
@@ -29,8 +45,10 @@ pushPlugin.register(this.pushSettings, (token: String) => {
2945
console.log((JSON.stringify(errorMessage));
3046
});
3147
```
32-
... could be rewriten using firebase like:
48+
... could be rewriten using Firebase plugin like:
3349
```js
50+
import * as firebase from "nativescript-plugin-firebase";
51+
3452
firebase.init({
3553
onMessageReceivedCallback: (message: firebase.Message) => {
3654
console.log(`Message: ${message}`);
@@ -40,18 +58,37 @@ firebase.init({
4058
}
4159
});
4260
```
43-
To test with sending messages, you can use the UI in Firebase Console, or use the `https://fcm.googleapis.com/fcm/send` API. See the [testing docs section in firebase plugin]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#testing").
61+
#### 5. Testing with messages
62+
To test with real messages, you can use the UI in Firebase Console, or use the `https://fcm.googleapis.com/fcm/send` API. See the [testing docs section in Firebase plugin]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#testing").
63+
64+
#### 6. Interactive Push Notifications - in push plugin they are set in the options argument passed to pushPlugin.register(options, callback) method. In Firebase plugin, it is done in a very similar way:
65+
66+
```js
67+
import * as firebase from "nativescript-plugin-firebase";
68+
import { messaging } from "nativescript-plugin-firebase/messaging";
69+
...
70+
const model = new messaging.PushNotificationModel();
71+
model.iosSettings = new messaging.IosPushSettings();
72+
model.iosSettings.interactiveSettings = new messaging.IosInteractivePushSettings();
73+
model.iosSettings.interactiveSettings.actions = [<<array of IosInteractiveNotificationAction>>]
74+
75+
model.iosSettings.interactiveSettings.categories = [{ identifier: "SOME CATEGORY" }];
76+
77+
model.onNotificationActionTakenCallback = () => {
78+
// callback to hook to if you want to handle what action have been taken by the user
79+
};
80+
81+
firebase.registerForInteractivePush(model: messaging.PushNotificationModel);
82+
```
83+
84+
Some lines in the above example have been omitted. See [Firebase plugin's interactive notifications docs]("https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#interactive-notifications-ios-only-for-now") for more details.
4485
45-
4.
86+
#### 7. areNotificationsEnabled() API
87+
In Firebase plugin it is pretty similar to the one in push-plugin, and even simpler to use:
4688
47-
-----------
89+
```js
90+
import * as firebase from "nativescript-plugin-firebase";
4891
49-
2. You can go to demos and see there but below are how some usages of push-plugin translate to firebase:
50-
51-
pushPlugin.areNotificationsEnabled(<some-callback-func>) -> firebase.areNotificationsEnabled(): boolean
52-
| push-plugin | nativescript-plugin-firebase |
53-
| :-------------: | :-------------: |
54-
| pushPlugin.areNotificationsEnabled(<some-callback-func>) | firebase.areNotificationsEnabled(): boolean |
55-
|
56-
pushPlugin.register(this.pushSettings)
57-
pushPlugin.registerUserNotificationSettings() | firebase.registerForInteractivePush(model) |
92+
const areTheyEnabled = firebase.areNotificationsEnabled(); // synchronous, retruns boolean;
93+
```
94+
This API is also supported in Android, SDK version 24 and above

0 commit comments

Comments
 (0)