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

Commit 16adca9

Browse files
Uncomment firebase-messaging in the plugin's include.gradle first #928
1 parent ceac307 commit 16adca9

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/messaging/messaging.android.ts

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,31 @@ import { MessagingOptions } from "../firebase";
77
declare const android, com, org: any;
88

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

1236
export function initFirebaseMessaging(options?: MessagingOptions) {
1337
if (!options) {
@@ -24,14 +48,6 @@ export function initFirebaseMessaging(options?: MessagingOptions) {
2448
export function onAppModuleLaunchEvent(args: any) {
2549
org.nativescript.plugins.firebase.FirebasePluginLifecycleCallbacks.registerCallbacks(appModule.android.nativeApp);
2650

27-
const senderIdResourceId = application.android.context.getResources().getIdentifier("gcm_defaultSenderId", "string", application.android.context.getPackageName());
28-
if (senderIdResourceId === 0) {
29-
console.log("####################### Seems like you did not include 'google-services.json' in your project! Firebase Messaging will not work properly. #######################");
30-
return;
31-
}
32-
33-
_senderId = application.android.context.getString(senderIdResourceId);
34-
3551
const intent = args.android;
3652
const isLaunchIntent = "android.intent.action.VIEW" === intent.getAction();
3753

@@ -72,18 +88,20 @@ export function registerForInteractivePush(model?: PushNotificationModel): void
7288
export function getCurrentPushToken(): Promise<string> {
7389
return new Promise((resolve, reject) => {
7490
try {
75-
if (typeof (com.google.firebase.messaging || com.google.firebase.iid) === "undefined" || _senderId === 0) {
91+
if (typeof (com.google.firebase.messaging || com.google.firebase.iid) === "undefined") {
7692
reject("Uncomment firebase-messaging in the plugin's include.gradle first");
7793
return;
7894
}
7995

80-
org.nativescript.plugins.firebase.FirebasePlugin.getCurrentPushToken(
81-
_senderId,
82-
new org.nativescript.plugins.firebase.FirebasePluginListener({
83-
success: token => resolve(token),
84-
error: err => reject(err)
85-
})
86-
);
96+
getSenderId().then(senderId => {
97+
org.nativescript.plugins.firebase.FirebasePlugin.getCurrentPushToken(
98+
senderId,
99+
new org.nativescript.plugins.firebase.FirebasePluginListener({
100+
success: token => resolve(token),
101+
error: err => reject(err)
102+
})
103+
);
104+
});
87105

88106
} catch (ex) {
89107
console.log("Error in messaging.getCurrentPushToken: " + ex);
@@ -138,13 +156,13 @@ export function addOnPushTokenReceivedCallback(callback) {
138156
export function registerForPushNotifications(options?: MessagingOptions): Promise<void> {
139157
return new Promise((resolve, reject) => {
140158
try {
141-
if (typeof (com.google.firebase.messaging) === "undefined" || _senderId === 0) {
159+
if (typeof (com.google.firebase.messaging) === "undefined") {
142160
reject("Uncomment firebase-messaging in the plugin's include.gradle first");
143161
return;
144162
}
145163

146164
initFirebaseMessaging(options);
147-
org.nativescript.plugins.firebase.FirebasePlugin.registerForPushNotifications(_senderId);
165+
getSenderId().then(senderId => org.nativescript.plugins.firebase.FirebasePlugin.registerForPushNotifications(senderId));
148166
} catch (ex) {
149167
console.log("Error in messaging.registerForPushNotifications: " + ex);
150168
reject(ex);
@@ -160,7 +178,7 @@ export function unregisterForPushNotifications(): Promise<void> {
160178
return;
161179
}
162180

163-
org.nativescript.plugins.firebase.FirebasePlugin.unregisterForPushNotifications(_senderId);
181+
getSenderId().then(senderId => org.nativescript.plugins.firebase.FirebasePlugin.unregisterForPushNotifications(senderId));
164182

165183
resolve();
166184
} catch (ex) {

0 commit comments

Comments
 (0)