diff --git a/native/android/app/build.gradle b/native/android/app/build.gradle --- a/native/android/app/build.gradle +++ b/native/android/app/build.gradle @@ -665,8 +665,8 @@ implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" implementation "com.google.android.gms:play-services-base:16.1.0" - implementation "com.google.firebase:firebase-core:16.0.9" - implementation "com.google.firebase:firebase-messaging:18.0.0" + implementation "com.google.firebase:firebase-core:21.1.0" + implementation "com.google.firebase:firebase-messaging:21.1.0" implementation "me.leolin:ShortcutBadger:1.1.21@aar" implementation project(':reactnativekeyboardinput') implementation "androidx.multidex:multidex:2.0.1" diff --git a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java --- a/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java +++ b/native/android/app/src/main/java/app/comm/android/notifications/CommAndroidNotifications.java @@ -12,6 +12,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; +import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.RemoteMessage; import java.util.HashMap; import java.util.Map; @@ -106,6 +107,17 @@ promise.resolve(enabled); } + @ReactMethod + public void getToken(Promise promise) { + FirebaseMessaging.getInstance().getToken().addOnCompleteListener(task -> { + if (task.isSuccessful()) { + promise.resolve(task.getResult()); + } else { + promise.reject(task.getException()); + } + }); + } + @Override public Map getConstants() { final Map constants = new HashMap<>(); diff --git a/native/push/android.js b/native/push/android.js --- a/native/push/android.js +++ b/native/push/android.js @@ -17,6 +17,7 @@ +setBadge: (count: number) => void, +removeAllDeliveredNotifications: () => void, +hasPermission: () => Promise, + +getToken: () => Promise, ... }; export type AndroidForegroundMessage = { diff --git a/native/push/push-handler.react.js b/native/push/push-handler.react.js --- a/native/push/push-handler.react.js +++ b/native/push/push-handler.react.js @@ -53,7 +53,6 @@ type AndroidForegroundMessage, CommAndroidNotifications, } from './android'; -import { getFirebase } from './firebase'; import InAppNotif from './in-app-notif.react'; import { requestIOSPushPermissions, @@ -61,8 +60,6 @@ } from './ios'; LogBox.ignoreLogs([ - // react-native-firebase - 'Require cycle: ../node_modules/react-native-firebase', // react-native-in-app-message 'ForceTouchGestureHandler is not available', ]); @@ -323,22 +320,21 @@ } async ensureAndroidPushNotifsEnabled() { - const firebase = getFirebase(); const hasPermission = await CommAndroidNotifications.hasPermission(); if (!hasPermission) { this.failedToRegisterPushPermissions(); return; } - const fcmToken = await firebase.messaging().getToken(); - if (fcmToken) { + try { + const fcmToken = await CommAndroidNotifications.getToken(); await this.handleAndroidDeviceToken(fcmToken); - } else { + } catch (e) { this.failedToRegisterPushPermissions(); } } - handleAndroidDeviceToken = async (deviceToken: ?string) => { + handleAndroidDeviceToken = async (deviceToken: string) => { this.registerPushPermissions(deviceToken); await this.handleInitialAndroidNotification(); };