/
index.android.ts
258 lines (221 loc) 路 7.28 KB
/
index.android.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { AndroidActivityNewIntentEventData, AndroidApplication, Application, Utils } from '@nativescript/core';
import { FirebaseApp, FirebaseError, firebase, deserialize } from '@nativescript/firebase-core';
import { AuthorizationStatus, IMessaging, RemoteMessage } from './common';
let defaultMessaging: Messaging;
export { AuthorizationStatus } from './common';
const fb = firebase();
Object.defineProperty(fb, 'messaging', {
value: () => {
if (!defaultMessaging) {
defaultMessaging = new Messaging();
}
return defaultMessaging;
},
writable: false,
});
let Callback;
function ensureCallback() {
@NativeClass
@Interfaces([org.nativescript.firebase.messaging.FirebaseMessaging.Callback])
class CallbackImpl extends java.lang.Object implements org.nativescript.firebase.messaging.FirebaseMessaging.Callback<string> {
_owner: WeakRef<Messaging>;
_propName: string;
constructor() {
super();
return global.__native(this);
}
public onError(error: any): void {}
public onSuccess(message: string): void {
const callback = this._owner?.get?.()?.[this._propName];
if (typeof callback === 'function') {
if (this._propName === '_onToken') {
callback(message);
} else {
try {
callback(JSON.parse(message));
} catch (e) { }
}
}
}
}
Callback = CallbackImpl;
}
export class Messaging implements IMessaging {
#native: com.google.firebase.messaging.FirebaseMessaging;
#app: FirebaseApp;
#onMessageCallback?;
#onMessage?: (message: RemoteMessage) => void;
#onNotificationTapCallback?;
#onNotificationTap?: (message: RemoteMessage) => void;
#onTokenCallback?;
#onToken?: (token: string) => void;
constructor() {
if (defaultMessaging) {
return defaultMessaging;
}
defaultMessaging = this;
org.nativescript.firebase.messaging.FirebaseMessaging.init(Utils.android.getApplicationContext());
this.#native = com.google.firebase.messaging.FirebaseMessaging.getInstance();
ensureCallback();
Application.android.on(AndroidApplication.activityNewIntentEvent, this._newIntentCallback);
}
showNotificationsWhenInForeground: boolean;
_newIntentCallback(args: AndroidActivityNewIntentEventData) {
org.nativescript.firebase.messaging.FirebaseMessaging.handleActivityIntent(args.intent);
}
get _onMessage() {
return this.#onMessage;
}
get _onMessageCallback() {
return this.#onMessageCallback;
}
get _onNotificationTap() {
return this.#onNotificationTap;
}
get _onNotificationTapCallback() {
return this.#onNotificationTapCallback;
}
get _onToken() {
return this.#onToken;
}
get _onTokenCallback() {
return this.#onTokenCallback;
}
getToken(): Promise<string> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.messaging.FirebaseMessaging.getToken(
this.#native,
new org.nativescript.firebase.messaging.FirebaseMessaging.Callback<string>({
onSuccess(result) {
resolve(result);
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
})
);
});
}
getAPNSToken(){
return null;
}
hasPermission(): Promise<AuthorizationStatus> {
return Promise.resolve(org.nativescript.firebase.messaging.FirebaseMessaging.hasPermission(Utils.android.getApplicationContext()) ? AuthorizationStatus.AUTHORIZED : AuthorizationStatus.DENIED);
}
onMessage(listener: (message: RemoteMessage) => any) {
this.#onMessage = listener;
if (listener) {
if (!this.#onMessageCallback) {
this.#onMessageCallback = new Callback();
this.#onMessageCallback._propName = '_onMessage';
}
this.#onMessageCallback._owner = new WeakRef(this);
org.nativescript.firebase.messaging.FirebaseMessaging.setOnMessageListener(this.#onMessageCallback);
}
}
onNotificationTap(listener: (message: RemoteMessage) => any) {
this.#onNotificationTap = listener;
if (listener) {
if (!this.#onNotificationTapCallback) {
this.#onNotificationTapCallback = new Callback();
this.#onNotificationTapCallback._propName = '_onNotificationTap';
}
this.#onNotificationTapCallback._owner = new WeakRef(this);
org.nativescript.firebase.messaging.FirebaseMessaging.setOnMessageTapListener(this.#onNotificationTapCallback);
}
}
onToken(listener: (token: string) => any) {
this.#onToken = listener;
if (listener) {
if (!this.#onTokenCallback) {
this.#onTokenCallback = new Callback();
this.#onTokenCallback._propName = '_onToken';
}
this.#onTokenCallback._owner = new WeakRef(this);
org.nativescript.firebase.messaging.FirebaseMessaging.setOnTokenListener(this.#onTokenCallback);
} else {
org.nativescript.firebase.messaging.FirebaseMessaging.setOnTokenListener(null);
}
}
registerDeviceForRemoteMessages(): Promise<void> {
return Promise.resolve();
}
requestPermission(permissions?: any): Promise<AuthorizationStatus> {
return this.hasPermission();
}
subscribeToTopic(topic: string): Promise<void> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.messaging.FirebaseMessaging.subscribeToTopic(
topic,
this.#native,
new org.nativescript.firebase.messaging.FirebaseMessaging.Callback<java.lang.Void>({
onSuccess(result) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
})
);
});
}
unregisterDeviceForRemoteMessages(): Promise<void> {
return Promise.resolve();
}
unsubscribeFromTopic(topic: string): Promise<void> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.messaging.FirebaseMessaging.unsubscribeFromTopic(
topic,
this.#native,
new org.nativescript.firebase.messaging.FirebaseMessaging.Callback<java.lang.Void>({
onSuccess(result) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
})
);
});
}
deleteToken(): Promise<void> {
return new Promise((resolve, reject) => {
org.nativescript.firebase.messaging.FirebaseMessaging.deleteToken(
this.#native,
new org.nativescript.firebase.messaging.FirebaseMessaging.Callback<java.lang.Void>({
onSuccess(result) {
resolve();
},
onError(error) {
reject(FirebaseError.fromNative(error));
},
})
);
});
}
get isDeviceRegisteredForRemoteMessages(): boolean {
return org.nativescript.firebase.messaging.FirebaseMessaging.hasPermission(Utils.android.getApplicationContext());
}
get autoInitEnabled(): boolean {
return this.native?.isAutoInitEnabled?.();
}
set autoInitEnabled(value) {
this.native?.setAutoInitEnabled?.(value);
}
get app(): FirebaseApp {
if (!this.#app) {
// @ts-ignore
this.#app = FirebaseApp.fromNative(FIRApp.defaultApp());
}
return this.#app;
}
get native() {
return this.#native;
}
get android() {
return this.native;
}
}
/*
curl -X POST --header "Authorization: key=AAAARAUJpsI:APA91bE1-kuJmZ5mfuYcX_1rdppafm5FhLaS4kzja4OBP8GN1Rl8VQ6wco12XUzyvXYv_7ggPfiaKMi8ntFfu1StqbxxdTSi_eOyvYhYLt6VfoJzvBqPrgDNGzu-34UQqWCWh5JrHyUz" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"badge\": \"1\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"cO6BvEjNRFWv4kAF_0THqB:APA91bFk0wojdAEOfQCm9zvLj36UwNmWIKzo0QsF92TRFJaqq8ZFPFI1CiCp-S1ZW6Jx6QPOjGiiGo61enSj6II0ja_w5-dp1Mie3to18kmI8kwx8uamezG0eHgi9_T3L8J-1tMv7JNL\"}"
*/