Skip to content

Commit 02baa51

Browse files
committed
fix: should fix callbacks being called twice
1 parent c1cb9a2 commit 02baa51

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

src/messaging.android.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,13 @@ import { MessagingOptions, PushNotificationModel } from './messaging';
44
declare const com;
55

66
let _launchNotification = null;
7-
// let _senderId: string = null;
8-
9-
// function getSenderId(): Promise<string> {
10-
// return new Promise((resolve, reject) => {
11-
// if (_senderId !== null) {
12-
// resolve(_senderId);
13-
// }
14-
15-
// const setSenderIdAndResolve = () => {
16-
// const senderIdResourceId = application.android.context
17-
// .getResources()
18-
// .getIdentifier('gcm_defaultSenderId', 'string', application.android.context.getPackageName());
19-
// if (senderIdResourceId === 0) {
20-
// throw new Error(
21-
// "####################### Seems like you did not include 'google-services.json' in your project! Firebase Messaging will not work properly. #######################"
22-
// );
23-
// }
24-
// _senderId = application.android.context.getString(senderIdResourceId);
25-
// resolve(_senderId);
26-
// };
27-
28-
// if (!application.android.context) {
29-
// // throw new Error("Don't call this function before your app has started.");
30-
// application.on(application.launchEvent, () => setSenderIdAndResolve());
31-
// } else {
32-
// setSenderIdAndResolve();
33-
// }
34-
// });
35-
// }
367

8+
let initialized = false;
379
async function initPushMessaging(options?: MessagingOptions) {
38-
if (!options) {
10+
if (!options || initialized) {
3911
return;
4012
}
13+
initialized = true;
4114
if (options.showNotificationsWhenInForeground !== undefined) {
4215
com.nativescript.push.PushMessagingService.showNotificationsWhenInForeground = options.showNotificationsWhenInForeground;
4316
}
@@ -111,7 +84,7 @@ export function getCurrentPushToken(): Promise<string> {
11184

11285
let _receivedNotificationCallback;
11386
function addOnMessageReceivedCallback(callback) {
114-
return new Promise((resolve, reject) => {
87+
return new Promise<void>((resolve, reject) => {
11588
try {
11689
_receivedNotificationCallback = callback;
11790

@@ -137,7 +110,7 @@ function addOnMessageReceivedCallback(callback) {
137110
}
138111

139112
function addOnPushTokenReceivedCallback(callback) {
140-
return new Promise((resolve, reject) => {
113+
return new Promise<void>((resolve, reject) => {
141114
try {
142115
let tokenReturned = false;
143116
com.nativescript.push.PushMessagingService.setOnPushTokenReceivedCallback(
@@ -220,7 +193,7 @@ export function unregisterForPushNotifications(): Promise<void> {
220193
}
221194

222195
export function subscribeToTopic(topicName) {
223-
return new Promise((resolve, reject) => {
196+
return new Promise<void>((resolve, reject) => {
224197
try {
225198
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
226199
onComplete: (task) => {
@@ -247,7 +220,7 @@ export function subscribeToTopic(topicName) {
247220
}
248221

249222
export function unsubscribeFromTopic(topicName) {
250-
return new Promise((resolve, reject) => {
223+
return new Promise<void>((resolve, reject) => {
251224
try {
252225
const onCompleteListener = new com.google.android.gms.tasks.OnCompleteListener({
253226
onComplete: (task) => {

src/messaging.ios.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ let _rejectWhenDidFailToRegisterForNotifications;
2828
// This way we can suppress the "Allow notifications" consent popup until the listeners are passed in.
2929
// const NOTIFICATIONS_REGISTRATION_KEY = 'Push-RegisterForRemoteNotifications';
3030

31-
export function initPushMessaging(options) {
32-
if (!options) {
31+
let initialized = false;
32+
async function initPushMessaging(options?: MessagingOptions) {
33+
if (!options || initialized) {
3334
return;
3435
}
36+
initialized = true;
3537
_showNotifications = options.showNotifications === undefined ? _showNotifications : !!options.showNotifications;
3638
_showNotificationsWhenInForeground =
3739
options.showNotificationsWhenInForeground === undefined
@@ -49,7 +51,7 @@ export function initPushMessaging(options) {
4951
}
5052

5153
export function addOnMessageReceivedCallback(callback: Function) {
52-
return new Promise((resolve, reject) => {
54+
return new Promise<void>((resolve, reject) => {
5355
try {
5456
// applicationSettings.setBoolean(NOTIFICATIONS_REGISTRATION_KEY, true);
5557

@@ -129,7 +131,7 @@ export function handleRemoteNotification(app, userInfo) {
129131
}
130132

131133
function addOnPushTokenReceivedCallback(callback) {
132-
return new Promise((resolve, reject) => {
134+
return new Promise<void>((resolve, reject) => {
133135
try {
134136
_receivedPushTokenCallback = callback;
135137
// may already be present
@@ -454,7 +456,7 @@ function _registerForRemoteNotifications(resolve?, reject?) {
454456
}
455457

456458
function _addOnNotificationActionTakenCallback(callback: Function) {
457-
return new Promise((resolve, reject) => {
459+
return new Promise<void>((resolve, reject) => {
458460
try {
459461
_notificationActionTakenCallback = callback;
460462
_processPendingActionTakenNotifications();

0 commit comments

Comments
 (0)