Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit a18743e

Browse files
#54 Support for Push Notifications
1 parent 7fd6235 commit a18743e

File tree

12 files changed

+314
-30
lines changed

12 files changed

+314
-30
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ For readability the supported features have been moved to their own README's:
8989
* [Realtime Database](docs/DATABASE.md)
9090
* [Authentication](docs/AUTHENTICATION.md)
9191
* [Remote Config](docs/REMOTECONFIG.md)
92-
* (Push) Notifications (work in progress)
93-
92+
* [Cloud Messaging](docs/MESSAGING.md)
9493

9594

9695
## Known issues on Android

docs/AUTHENTICATION.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ Don't forget to enable Facebook login in your firebase instance.
189189
)
190190
```
191191

192-
And also, add [this bit to `app.js`](https://github.com/EddyVerbruggen/nativescript-plugin-firebase-demo/blob/master/Firebase/app/app.js#L5-L34) to give control back to your app after Facebook's auth UI finished.
192+
#### Start-up wiring
193+
We need to do some wiring when your app starts, so open `app.js` and add this before `application.start();`:
193194

195+
```js
196+
var firebase = require("nativescript-plugin-firebase");
197+
```
198+
199+
_Note that if you previously added some other code for this plugin to `app.js` you can now go ahead and remove it._
194200

195201
### logout
196202
Shouldn't be more complicated than:

docs/MESSAGING.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<table>
2+
<tr>
3+
<td><img src="images/firebase.png" width="154px" height="43px" alt="Firebase"/></td>
4+
<td>Cloud Messaging</td>
5+
</tr>
6+
</table>
7+
8+
## Enabling Firebase Cloud Messaging (FCM)
9+
Version 3.3.0 of this plugin added FCM support (which is the successor of GCM).
10+
11+
Although using push messages in your Firebase app is really easy setting it up is not. Traditionally, especially for iOS.
12+
13+
### Android
14+
Work in progress.
15+
16+
### iOS
17+
18+
#### Receiving remote notifications in the background
19+
Open `app/App_Resources/iOS/Info.plist` and add this to the bottom:
20+
21+
```xml
22+
<key>UIBackgroundModes</key>
23+
<array>
24+
<string>remote-notification</string>
25+
</array>
26+
```
27+
28+
#### Provisioning hell
29+
Follow [this guide](https://firebase.google.com/docs/cloud-messaging/ios/certs) to the letter. Once you've done it run `tns run ios` and upon starting the app it should prompt you for notification support. That also works on the simulator, but actually receiving notifications is _only_ possible on a real device.
30+
31+
## Functions
32+
I tried to make this as simple as possible, so everything is handled for you transparently. If you want to act upon the notification that triggered opening your app then configure a callback handler as follows.
33+
34+
### Handling a notification
35+
To listen to received notifications while in the foreground or when your app moves from the background to the foreground, add a handler `init`.
36+
37+
Any pending notifications (while your app was not in the foreground) will trigger the `onMessageReceivedCallback` handler.
38+
39+
```js
40+
firebase.init({
41+
onMessageReceivedCallback: function(message) {
42+
console.log("Title: " + message.title);
43+
console.log("Body: " + message.body);
44+
// if your server passed a custom property called 'foo', then do this:
45+
console.log("Value of 'foo': " + message.foo);
46+
});
47+
```
48+
49+
You don't _have_ to provide the handler during `init` - you can also do it through a dedicated function.
50+
51+
```js
52+
firebase.addOnMessageReceivedCallback(
53+
function(message) {
54+
// ..
55+
});
56+
});
57+
```

docs/NOTIFICATIONS.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

firebase-common.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ declare module "nativescript-plugin-firebase" {
2121
/**
2222
* This requires you to setup Facebook Auth in the Firebase console,
2323
* as well as uncommenting the SDK includes in include.gradle (Android) and Podfile (iOS).
24-
*
25-
* Note that this works well on iOS, but Android is work in progress.
2624
*/
2725
FACEBOOK
2826
}

firebase-common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ firebase.firebaseRemoteConfig = null;
3131

3232
firebase.authStateListeners = [];
3333

34+
firebase._receivedNotificationCallback = null;
35+
36+
firebase._pendingNotifications = [];
37+
3438
firebase.addAuthStateListener = function(listener) {
3539
if (firebase.authStateListeners.indexOf(listener) === -1) {
3640
firebase.authStateListeners.push(listener);

firebase.android.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ firebase.init = function (arg) {
132132
firebaseAuth.addAuthStateListener(firebase.authStateListener);
133133
}
134134

135+
// Firebase notifications (FCM)
136+
if (typeof(com.google.firebase.messaging) !== "undefined") {
137+
console.log("--- has messaging!");
138+
// TODO see iOS:
139+
// firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification);
140+
if (arg.onMessageReceivedCallback !== undefined) {
141+
console.log("--- adding messaging callback!");
142+
firebase.addOnMessageReceivedCallback(arg.onMessageReceivedCallback);
143+
}
144+
}
145+
135146
resolve(firebase.instance);
136147
} catch (ex) {
137148
console.log("Error in firebase.init: " + ex);
@@ -140,6 +151,24 @@ firebase.init = function (arg) {
140151
});
141152
};
142153

154+
firebase.addOnMessageReceivedCallback = function (callback) {
155+
return new Promise(function (resolve, reject) {
156+
try {
157+
if (typeof(com.google.firebase.messaging) === "undefined") {
158+
reject("Uncomment firebase-messaging in the plugin's include.gradle first");
159+
return;
160+
}
161+
162+
firebase._receivedNotificationCallback = callback;
163+
firebase._processPendingNotifications();
164+
resolve();
165+
} catch (ex) {
166+
console.log("Error in firebase.addOnMessageReceivedCallback: " + ex);
167+
reject(ex);
168+
}
169+
});
170+
};
171+
143172
firebase.getRemoteConfigDefaults = function (properties) {
144173
var defaults = {};
145174
for (var p in properties) {
@@ -340,7 +369,7 @@ firebase.login = function (arg) {
340369

341370
} else if (arg.type === firebase.LoginType.FACEBOOK) {
342371
if (typeof(com.facebook) === "undefined") {
343-
reject("Facebook SDK not installed - see Podfile");
372+
reject("Facebook SDK not installed - see gradle config");
344373
return;
345374
}
346375

firebase.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,31 @@ declare module "nativescript-plugin-firebase" {
201201
properties: Object;
202202
}
203203

204+
/**
205+
* The returned object in the callback handler of the addOnMessageReceivedCallback function.
206+
* Note that any custom data you send from your server will be available as
207+
* key/value properties on the Message object.
208+
*/
209+
export interface Message {
210+
/**
211+
* The main text shown in the notificiation.
212+
*/
213+
body: string;
214+
/**
215+
* Optional title, shown above the body in the notification.
216+
*/
217+
title?: string;
218+
/**
219+
* iOS badge number
220+
*/
221+
badge?: number;
222+
}
223+
204224
export function init(options: InitOptions): Promise<any>;
205225
export function login(options: LoginOptions): Promise<LoginResult>;
206226
export function logout(): Promise<any>;
207227
export function getRemoteConfig(options: GetRemoteConfigOptions): Promise<GetRemoteConfigResult>;
228+
export function addOnMessageReceivedCallback(onMessageReceived: (data: Message) => void): Promise<any>;
208229
export function createUser(options: CreateUserOptions): Promise<CreateUserResult>;
209230
export function deleteUser(): Promise<any>;
210231
export function resetPassword(options: ResetPasswordOptions): Promise<any>;

0 commit comments

Comments
 (0)