This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathpush-view-model.ts
225 lines (205 loc) · 7.59 KB
/
push-view-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { Observable } from "tns-core-modules/data/observable";
import * as firebase from "nativescript-plugin-firebase";
import { messaging } from "nativescript-plugin-firebase/messaging";
import { alert, confirm } from "tns-core-modules/ui/dialogs";
import * as platform from "tns-core-modules/platform";
import * as applicationSettings from "tns-core-modules/application-settings";
const getCircularReplacer = () => {
const seen = new WeakSet;
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
export class PushViewModel extends Observable {
private static APP_REGISTERED_FOR_NOTIFICATIONS = "APP_REGISTERED_FOR_NOTIFICATIONS";
constructor() {
super();
if (applicationSettings.getBoolean(PushViewModel.APP_REGISTERED_FOR_NOTIFICATIONS, false)) {
this.doRegisterPushHandlers();
}
}
public doRequestConsent(): void {
confirm({
title: "We'd like to send notifications",
message: "Do you agree? Please do, we won't spam you. Promised.",
okButtonText: "Yep!",
cancelButtonText: "Nooo"
}).then(pushAllowed => {
if (pushAllowed) {
applicationSettings.setBoolean(PushViewModel.APP_REGISTERED_FOR_NOTIFICATIONS, true);
this.doRegisterPushHandlers();
}
});
}
public doGetCurrentPushToken(): void {
firebase.getCurrentPushToken()
.then(token => {
// may be null/undefined if not known yet
alert({
title: "Current Push Token",
message: (!token ? "Not received yet (note that on iOS this does not work on a simulator)" : token + ("\n\nSee the console log if you want to copy-paste it.")),
okButtonText: "OK, thx"
});
})
.catch(err => console.log("Error in doGetCurrentPushToken: " + err));
}
public doRegisterForInteractivePush(): void {
if (!platform.isIOS) {
console.log("##### Interactive push messaging is currently iOS-only!");
console.log("##### Also, please make sure you don't include the 'click_action' notification property when pusing to Android.");
}
const model = new messaging.PushNotificationModel();
model.iosSettings = new messaging.IosPushSettings();
model.iosSettings.badge = false;
model.iosSettings.alert = true;
model.iosSettings.interactiveSettings = new messaging.IosInteractivePushSettings();
model.iosSettings.interactiveSettings.actions = [
{
identifier: "OPEN_ACTION",
title: "Open the app (if closed)",
options: messaging.IosInteractiveNotificationActionOptions.foreground
},
{
identifier: "AUTH",
title: "Open the app, but only if device is not locked with a passcode",
options: messaging.IosInteractiveNotificationActionOptions.foreground | messaging.IosInteractiveNotificationActionOptions.authenticationRequired
},
{
identifier: "INPUT_ACTION",
title: "Tap to reply without opening the app",
type: "input",
submitLabel: "Fire!",
placeholder: "Load the gun..."
},
{
identifier: "INPUT_ACTION",
title: "Tap to reply and open the app",
options: messaging.IosInteractiveNotificationActionOptions.foreground,
type: "input",
submitLabel: "OK, send it",
placeholder: "Type here, baby!"
},
{
identifier: "DELETE_ACTION",
title: "Delete without opening the app",
options: messaging.IosInteractiveNotificationActionOptions.destructive
}
];
model.iosSettings.interactiveSettings.categories = [{
identifier: "GENERAL"
}];
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message, inputText?: string) => {
console.log(`onNotificationActionTakenCallback fired! \n\r Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`);
alert({
title: "Interactive push action",
message: `Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}` + (inputText ? `, \n\r Input text: ${inputText}` : ""),
okButtonText: "Nice!"
});
};
firebase.registerForInteractivePush(model);
console.log("Registered for interactive push");
alert({
title: "Registered for interactive push",
okButtonText: "Thx!"
});
}
// You would normally add these handlers in 'init', but if you want you can do it seperately as well.
// The benefit being your user will not be confronted with the "Allow notifications" consent popup when 'init' runs.
public doRegisterPushHandlers(): void {
// note that this will implicitly register for push notifications, so there's no need to call 'registerForPushNotifications'
firebase.addOnPushTokenReceivedCallback(
token => {
// you can use this token to send to your own backend server,
// so you can send notifications to this specific device
console.log("Firebase plugin received a push token: " + token);
// var pasteboard = utils.ios.getter(UIPasteboard, UIPasteboard.generalPasteboard);
// pasteboard.setValueForPasteboardType(token, kUTTypePlainText);
}
);
firebase.addOnMessageReceivedCallback(
message => {
console.log("Push message received in push-view-model: " + JSON.stringify(message, getCircularReplacer()));
setTimeout(() => {
alert({
title: "Push message!",
message: (message !== undefined && message.title !== undefined ? message.title : ""),
okButtonText: "Sw33t"
});
}, 500);
}
).then(() => {
console.log("Added addOnMessageReceivedCallback");
}, err => {
console.log("Failed to add addOnMessageReceivedCallback: " + err);
});
}
public doUnregisterForPushNotifications(): void {
firebase.unregisterForPushNotifications().then(
() => {
alert({
title: "Unregistered",
message: "If you were registered, that is.",
okButtonText: "Got it, thanks!"
});
});
}
public doRegisterForPushNotificationsAgain(): void {
firebase.registerForPushNotifications().then(
() => {
alert({
title: "Registered again",
message: "You should now use the new push token which was received in 'addOnPushTokenReceivedCallback, or call 'getCurrentPushToken'.",
okButtonText: "Got it."
});
});
}
public doSubscribeToTopic(): void {
firebase.subscribeToTopic("demo").then(
() => {
alert({
title: "Subscribed",
message: ".. to the 'demo' topic",
okButtonText: "Okay, interesting"
});
},
error => {
alert({
title: "Subscribe error",
message: error,
okButtonText: "OK"
});
}
);
}
public doUnsubscribeFromTopic(): void {
firebase.unsubscribeFromTopic("demo").then(
() => {
alert({
title: "Unsubscribed",
message: ".. from the 'demo' topic",
okButtonText: "Okay, very interesting"
});
},
error => {
alert({
title: "Unsubscribe error",
message: error,
okButtonText: "OK"
});
}
);
}
public doGetAreNotificationsEnabled(): void {
alert({
title: "AreNotificationsEnabled",
message: "" + firebase.areNotificationsEnabled(),
okButtonText: "Okay, very interesting"
});
}
}