cordova-plugin-firebase: Ionic App freezes on launch after notification received on iOS

@cordova 8.0.0 @cordova-ios 4.5.4

Hi, I have a problem on iOS on my ionic (v1) application when I receive a notification and the application is closed. The small “1” badge is well-displayed on the icon. But when I click on the notification (or when I open the app simply by clicking on the app icon), randomly the apps freezes on the splash screen. The problem is that sometimes the app opens well and sometimes it freezes and I haven’t identified the reason.

On top of that I haven’t manage to catch error messages so far in the console. Every time I try to catch this bug, actually the app opens well…

I thought that maybe the Firebase plugin was not loaded, so I put lots of safety measures and “if” statements in my code but it stills freezes.

This is an extract of my code on notifications management in my main controller :

var firebase_ready = function() { 
   if(typeof cordova != "undefined" && cordova && typeof window.FirebasePlugin != "undefined" && window.FirebasePlugin) { 
         try { 
             var saveToken = function(token) {  // Save token 
                   if(token !== null && token !== ''){ 
                       window.localStorage.setItem('firebase_token',token);
                       onNotificationOpen(); 
                    } 
               }; 

            var errorToken = function(err) {   // Error token
                console.log('error retrieving firebase token: ' + err); 
            }; 

           var manageToken = function(success) { 
                 if(success) { 
                       window.FirebasePlugin.getToken(saveToken, errorToken); 
                       window.FirebasePlugin.onTokenRefresh(saveToken, errorToken); 
                 } 
           }; 

          var onNotificationOpen = function() { 
                window.FirebasePlugin.onNotificationOpen(function(data){ 
                     //Notification received on device tray and tapped by the user. [In background] 
                     if(typeof data != "undefined" && data && data.tap){ 
                          console.log('Notification BG'); 
                     } else if(typeof data != "undefined" && data){ // App in foreground
                               console.log('Notification FG'); 
                               if(typeof data.aps != "undefined" && data.aps && data.aps.badge) { 
                                    var badgeNum = angular.isNumber(data.aps.badge) ? parseInt(data.aps.badge) : 0; 
                                     window.FirebasePlugin.setBadgeNumber(badgeNum); 
                               } else if(typeof data.notification != "undefined" && data.notification && data.notification.badge) {
                                    var badgeNum = angular.isNumber(data.notification.badge) ? parseInt(data.notification.badge) : 0; 
                                    window.FirebasePlugin.setBadgeNumber(badgeNum); 
                             } 
                      } 
                }, function(msg){  console.log('onNotification callback successfully registered: ' + msg);  }, 
                    function(err){  console.log('Error registering onNotification callback: ' + err); } 
               ); 
           }; 

      if($ionicPlatform.is('ios')) { 
          window.FirebasePlugin.hasPermission(function(data){ 
               if(!data.isEnabled) { 
                   window.FirebasePlugin.grantPermission(manageToken, errorToken); 
                } else { 
                   manageToken(true); 
                } 
           }); 
       } 
    } catch(e) { console.log('Error firebase: ' + e); } 
  } 
}; 

$ionicPlatform.ready(function() { 
    $timeout(function() { // I tried with a timeout here without any success
        firebase_ready(); 
     },200); 
});

Do you know what happends ? And why it is random ? Thanks

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 29 (7 by maintainers)

Most upvoted comments

@svzi I am really glad to hear you got it worked out!