Skip to content

Commit

Permalink
fix(messaging): use weakref
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Sep 3, 2022
1 parent b2034d1 commit 4f687d1
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/firebase-messaging-core/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ const onMessageCallbacks: Set<(message: any) => void> = new Set();
const onTokenCallbacks: Set<(token: any) => void> = new Set();
const onNotificationTapCallbacks: Set<(message: any) => void> = new Set();

let lastActivity;
let lastActivity: WeakRef<androidx.appcompat.app.AppCompatActivity>;
let requestPermissionLauncher: androidx.activity.result.ActivityResultLauncher<any>;
let _resolve;

function register(args: any) {
if (!lastActivity) {
lastActivity = args.activity;
lastActivity = new WeakRef(args.activity);
requestPermissionLauncher = args.activity.registerForActivityResult(
new androidx.activity.result.contract.ActivityResultContracts.RequestPermission(),
new androidx.activity.result.ActivityResultCallback({
Expand Down Expand Up @@ -142,8 +142,8 @@ export class MessagingCore implements IMessagingCore {
Application.android.once('activityCreated', register);

Application.android.on('activityDestroyed', (args) => {
const activity = args.activity;
if (lastActivity && activity === lastActivity) {
const activity = lastActivity?.get?.();
if (activity && args.activity === activity) {
requestPermissionLauncher?.unregister?.();
lastActivity = undefined;
Application.android.once('activityCreated', register);
Expand Down

0 comments on commit 4f687d1

Please sign in to comment.