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

Commit 19f46ad

Browse files
feature: [iOS] enable handling of text input notifications #886
1 parent 242dd69 commit 19f46ad

File tree

9 files changed

+196
-146
lines changed

9 files changed

+196
-146
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
[Firebase iOS SDK Changelog](https://firebase.google.com/support/release-notes/ios)
44
[Firebase Android SDK Changelog](https://firebase.google.com/support/release-notes/android)
55

6+
## 6.7.0 (2018, September X)
7+
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/67?closed=1)
8+
9+
610
## 6.6.0 (2018, August 28)
711
[Fixes & Enhancements](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/milestone/66?closed=1)
812

demo/app/messaging-view-model.ts

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,40 @@ export class MessagingViewModel {
3939
model.iosSettings.interactiveSettings.actions = [
4040
{
4141
identifier: "OPEN_ACTION",
42-
title: "Open the app",
42+
title: "Open the app (if closed)",
4343
options: messaging.IosInteractiveNotificationActionOptions.foreground
4444
},
4545
{
4646
identifier: "AUTH",
47-
title: "Not on lock screen",
48-
options: messaging.IosInteractiveNotificationActionOptions.authenticationRequired
47+
title: "Open the app, but only if device is not locked with a passcode",
48+
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.authenticationRequired
4949
},
5050
{
5151
identifier: "INPUT_ACTION",
52-
title: "Reply",
53-
options: messaging.IosInteractiveNotificationActionOptions.foreground,
52+
title: "Tap to reply without opening the app",
5453
type: "input",
5554
submitLabel: "Fire!",
5655
placeholder: "Load the gun..."
5756
},
5857
{
59-
identifier: "DELETE_ACTION",
60-
title: "Delete and open",
61-
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.destructive
62-
}
63-
];
64-
model.iosSettings.interactiveSettings.categories = [
58+
identifier: "INPUT_ACTION",
59+
title: "Tap to reply and open the app",
60+
options: messaging.IosInteractiveNotificationActionOptions.foreground,
61+
type: "input",
62+
submitLabel: "OK, send it",
63+
placeholder: "Type here, baby!"
64+
},
6565
{
66-
identifier: "GENERAL"
66+
identifier: "DELETE_ACTION",
67+
title: "Delete without opening the app",
68+
options: messaging.IosInteractiveNotificationActionOptions.destructive
6769
}
6870
];
6971

72+
model.iosSettings.interactiveSettings.categories = [{
73+
identifier: "GENERAL"
74+
}];
75+
7076
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message, inputText?: string) => {
7177
console.log(`onNotificationActionTakenCallback fired! \n\r Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`);
7278

@@ -78,6 +84,7 @@ export class MessagingViewModel {
7884
};
7985

8086
firebase.registerForInteractivePush(model);
87+
console.log("Registered for interactive push");
8188
} else {
8289
console.log("Interactive push messaging is iOS-only!");
8390
}
@@ -87,26 +94,26 @@ export class MessagingViewModel {
8794
// The benefit being your user will not be confronted with the "Allow notifications" consent popup when 'init' runs.
8895
public doRegisterPushHandlers(): void {
8996
firebase.addOnPushTokenReceivedCallback(
90-
token => {
91-
// you can use this token to send to your own backend server,
92-
// so you can send notifications to this specific device
93-
console.log("Firebase plugin received a push token: " + token);
94-
// var pasteboard = utils.ios.getter(UIPasteboard, UIPasteboard.generalPasteboard);
95-
// pasteboard.setValueForPasteboardType(token, kUTTypePlainText);
96-
}
97+
token => {
98+
// you can use this token to send to your own backend server,
99+
// so you can send notifications to this specific device
100+
console.log("Firebase plugin received a push token: " + token);
101+
// var pasteboard = utils.ios.getter(UIPasteboard, UIPasteboard.generalPasteboard);
102+
// pasteboard.setValueForPasteboardType(token, kUTTypePlainText);
103+
}
97104
);
98105
firebase.addOnMessageReceivedCallback(
99-
message => {
100-
console.log("------------------- push message received: " + JSON.stringify(message, getCircularReplacer()));
106+
message => {
107+
console.log("------------------- push message received: " + JSON.stringify(message, getCircularReplacer()));
101108

102-
setTimeout(() => {
103-
alert({
104-
title: "Push message!",
105-
message: (message.title !== undefined ? message.title : ""),
106-
okButtonText: "Sw33t"
107-
});
108-
}, 500);
109-
}
109+
setTimeout(() => {
110+
alert({
111+
title: "Push message!",
112+
message: (message.title !== undefined ? message.title : ""),
113+
okButtonText: "Sw33t"
114+
});
115+
}, 500);
116+
}
110117
).then(() => {
111118
console.log("Added addOnMessageReceivedCallback");
112119
}, err => {
@@ -116,50 +123,50 @@ export class MessagingViewModel {
116123

117124
public doUnregisterForPushNotifications(): void {
118125
firebase.unregisterForPushNotifications().then(
119-
() => {
120-
alert({
121-
title: "Unregistered",
122-
message: "If you were registered, that is.",
123-
okButtonText: "Got it, thanks!"
126+
() => {
127+
alert({
128+
title: "Unregistered",
129+
message: "If you were registered, that is.",
130+
okButtonText: "Got it, thanks!"
131+
});
124132
});
125-
});
126133
}
127134

128135
public doSubscribeToTopic(): void {
129136
firebase.subscribeToTopic("demo").then(
130-
() => {
131-
alert({
132-
title: "Subscribed",
133-
message: ".. to the 'demo' topic",
134-
okButtonText: "Okay, interesting"
135-
});
136-
},
137-
error => {
138-
alert({
139-
title: "Subscribe error",
140-
message: error,
141-
okButtonText: "OK"
142-
});
143-
}
137+
() => {
138+
alert({
139+
title: "Subscribed",
140+
message: ".. to the 'demo' topic",
141+
okButtonText: "Okay, interesting"
142+
});
143+
},
144+
error => {
145+
alert({
146+
title: "Subscribe error",
147+
message: error,
148+
okButtonText: "OK"
149+
});
150+
}
144151
);
145152
}
146153

147154
public doUnsubscribeFromTopic(): void {
148155
firebase.unsubscribeFromTopic("demo").then(
149-
() => {
150-
alert({
151-
title: "Unsubscribed",
152-
message: ".. from the 'demo' topic",
153-
okButtonText: "Okay, very interesting"
154-
});
155-
},
156-
error => {
157-
alert({
158-
title: "Unsubscribe error",
159-
message: error,
160-
okButtonText: "OK"
161-
});
162-
}
156+
() => {
157+
alert({
158+
title: "Unsubscribed",
159+
message: ".. from the 'demo' topic",
160+
okButtonText: "Okay, very interesting"
161+
});
162+
},
163+
error => {
164+
alert({
165+
title: "Unsubscribe error",
166+
message: error,
167+
okButtonText: "OK"
168+
});
169+
}
163170
);
164171
}
165172

docs/MESSAGING.md

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ This results in a payload of:
193193

194194
### Interactive notifications (iOS only for now)
195195
To register the app to receive interactive pushes you need to call `firebase.registerForInteractivePush(model)`.
196-
And you may hook to the `model.onNotificationActionTakenCallback` callback to know what action the user took interacting with the notification:
196+
And you may hook to the `model.onNotificationActionTakenCallback` callback to know what action the user took interacting with the notification.
197+
198+
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/messaging/interactive01.png" height="270px" alt="Interactive Notification, part 1"/> <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/messaging/interactive02.png" height="270px" alt="Interactive Notification, part 2"/> <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/messaging/interactive03.png" height="270px" alt="Interactive Notification, part 3"/> <img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/messaging/interactive04.png" height="270px" alt="Interactive Notification, part 4"/>
199+
200+
The example shown above was created with the code below.
197201

198202
```typescript
199203
import { messaging } from "nativescript-plugin-firebase/messaging";
@@ -207,18 +211,33 @@ model.iosSettings.interactiveSettings = new messaging.IosInteractivePushSettings
207211
model.iosSettings.interactiveSettings.actions = [
208212
{
209213
identifier: "OPEN_ACTION",
210-
title: "Open the app",
214+
title: "Open the app (if closed)",
211215
options: messaging.IosInteractiveNotificationActionOptions.foreground
212216
},
213217
{
214218
identifier: "AUTH",
215-
title: "Not on lock screen",
216-
options: messaging.IosInteractiveNotificationActionOptions.authenticationRequired
219+
title: "Open the app, but only if device is not locked with a passcode",
220+
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.authenticationRequired
221+
},
222+
{
223+
identifier: "INPUT_ACTION",
224+
title: "Tap to reply without opening the app",
225+
type: "input",
226+
submitLabel: "Fire!",
227+
placeholder: "Load the gun..."
228+
},
229+
{
230+
identifier: "INPUT_ACTION",
231+
title: "Tap to reply and open the app",
232+
options: messaging.IosInteractiveNotificationActionOptions.foreground,
233+
type: "input",
234+
submitLabel: "OK, send it",
235+
placeholder: "Type here, baby!"
217236
},
218237
{
219238
identifier: "DELETE_ACTION",
220-
title: "Delete and open",
221-
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.destructive
239+
title: "Delete without opening the app",
240+
options: messaging.IosInteractiveNotificationActionOptions.destructive
222241
}
223242
];
224243

@@ -233,10 +252,27 @@ model.onNotificationActionTakenCallback = (actionIdentifier: string, message: fi
233252
firebase.registerForInteractivePush(model);
234253
```
235254

236-
To send an interactive push, add the `"click_action"` property to the notification, with a value corresponding to the `category` defined in the model you've registered in the app:
255+
To send an interactive push, add the `"click_action"` property to the notification, with a value corresponding to the `category` defined in the model you've registered in the app.
256+
The payload to trigger the notification in the screenshots above is:
237257

238-
```bash
239-
curl -X POST --header "Authorization: key=AAAA9SHtZvM:APA91bGoY0H2nS8GlzzypDXSiUkNY3nrti4st4WOUs_w1A0Rttcx31U90YGv-p3U4Oql-vh-FzZzWUUPEwl47uvwhI4tB5yz4wwzrJA2fVqLEKZpDU42AQppYnU2-dsURqkyc9sKcjay2egWbfyNK2b-G2JQCqrLVA" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"notification\": {\"title\": \"My title\", \"text\": \"My text\", \"click_action\":\"GENERAL\"}, \"content_available\":true, \"to\": \"exbKSYOGbto:APA91bHqFX9EA6SxY7NkVKV3ajea9xYn9_2dPz2jS7DGuymoE3fMDhPZLVbTXxbQ5_tS6nxmjdmfAEACM4_L-egNneXInuvg8JfRjrCVICTa8vnccTBq8cAnIx6cME1FvER9WIDC3dC4\"}"
258+
```json
259+
{
260+
"notification": {
261+
"title": "I DEMAND YOUR ATTENTION",
262+
"subtitle": "Just kidding, but not really",
263+
"text": "Sorry to bother you I meant, please pick an option below..",
264+
"click_action": "GENERAL",
265+
"badge": "1",
266+
"sound": "default",
267+
"showWhenInForeground": true
268+
},
269+
"content_available": false,
270+
"data": {
271+
"foo": "bar"
272+
},
273+
"priority": "High",
274+
"to": "DEVICE_PUSH_KEY>"
275+
}
240276
```
241277

242278
### (iOS) showing a notification while the app is in the foreground
2.52 MB
Loading
2.06 MB
Loading
1.14 MB
Loading
512 KB
Loading

0 commit comments

Comments
 (0)