andrehtissot / cordova-plugin-fcm-with-dependecy-updated

Google FCM Push Notifications Cordova Plugin
MIT License
209 stars 278 forks source link

I can't getting token value by this getToken() method in iOS devices when app is kill #142

Open vijaySlk opened 4 years ago

vijaySlk commented 4 years ago
  1. getToken() call after login
  2. when my ios app kill then i have getting token null value and if not kill app and login - logout multiple times then i have getting token value
  3. ios devices version for testing 12.4
  4. plugin and npm
    • cordova-plugin-fcm-with-dependecy-updated": "^6.4.3
    • @ionic-native/fcm": "^5.27.0
  5. below my code

    this.fcm.hasPermission().then(hasPermission => {
    console.log('hasPermission_token...', hasPermission);
    
    if (hasPermission) {
        console.log("Has permission for fcm token!");
        setTimeout(() => {
            this.fcm.getToken().then(token => {
                console.log('this is fcm new token', token);
                ///call api save fcm to backend
                this.FCMTokenSave(token, loginUserDetail);
    
            }).catch(err => {
                console.log('fcmtoken_err1', err);
    
            });
        }, 500);
    
        this.fcm.onTokenRefresh().subscribe(token => {
            console.log('this is fcm refresh token', token);
            ///call api save fcm to backend
            this.FCMTokenSave(token, loginUserDetail);
        });
    } else {
        this.toasthelper.presentToast('Please give permission for Notification');
        const w: any = window;
        w.FCMPlugin.requestPushPermissionIOS(() => {
            console.log('push permissions success');
    
            setTimeout(() => {
                this.fcm.getToken().then(token => {
                    console.log('this is fcm new token', token);
                    ///call api save fcm to backend
                    this.FCMTokenSave(token, loginUserDetail);
    
                }).catch(err => {
                    console.log('fcmtoken_err2', err);
    
                });
            }, 500);
            this.fcm.onTokenRefresh().subscribe(token => {
                console.log('this is fcm refresh token', token);
                ///call api save fcm to backend
                this.FCMTokenSaveRefresh(token, loginUserDetail);
            });
        }, (e) => {
            console.log('push permissions fail', e);
            this.toasthelper.presentToast(e.message);
        });
    }
    }).catch(err => {
    console.log('hasPermission_err...', err);
    })
andrehtissot commented 4 years ago

Hi @vijaySlk.

Would you explain what do you mean by "app kill"?

If the app is killed, it means that is not running, right?

vijaySlk commented 4 years ago

app kill means app not running is right but after kill again app open then token value getting null

andrehtissot commented 4 years ago

It makes sense. Because, this token is provided by the SDK, so if it is not yet re-fetched, the value would be null. Would you try something like this:

const getValidToken = () => new Promise((resolve, reject) => {
    const success = (token: string) => {
        if(!token) { return }
        this.fcm.onTokenRefresh().unsubscribe(success);
        resolve(token);
    }
    this.fcm.getToken().then(success).catch(reject);
    this.fcm.onTokenRefresh().subscribe(success);
})

Then calling getValidToken, would not resolve until a non-null value is retrieved.

vijaySlk commented 4 years ago

now i have find one solution remove below plugin ionic cordova plugin add cordova-plugin-firebase-crashlytics npm install @ionic-native/firebase-crashlytics

this plugin conflict to FCM

andrehtissot commented 4 years ago

@vijaySlk Conflicts are never good. Would you share which version you had installed? Just keep a note and check later.

UsamaParkar commented 4 years ago

Hi

@vijaySlk Conflicts are never good. Would you share which version you had installed? Just keep a note and check later.

Hi, I'm experiencing a similar issue where the token is sometimes null, on IOS. I believe your solution regarding promise resolve and reject could be the answer, but I want to under the steps inside the code, since I have some additional steps of my own. I would really appreciate it, if you would help me out with this.

andrehtissot commented 4 years ago

@UsamaParkar I didn't understand what you meant by I want to under the steps inside the code, since I have some additional steps of my own.