I want to generate a token for firebase cloud messaging ( push 
Notifications) 

The filter code:


app. factory('Notification', function($rootScope,$q) {
  var Service = {};
  Service.getTokenKey = getTokenKey;
  Service.getFirebaseToken = getFirebaseToken;
  const messaging = firebase.messaging();
  function getTokenKey(){
    messaging.requestPermission()
      .then(function() {
        console.log('Notification permission granted.');
        getFirebaseToken();

      })
      .catch(function(err) {
        console.log('Unable to get permission to notify.', err);
      });

  }
  function getFirebaseToken(){
    var token = messaging.getToken();
    return token;
  }
  return Service;

})


Controller Code:

Notification.getFirebaseToken().then(function(res) {
    $scope.token = res;
    console.log('CALLED', res);
    MyService.registertoken({"token":[$scope.token]})
      .then(function (response) {
      });
  });



After the User Logged in window will appear for subscription "permission"  
after allowing  the value of "res" is undefined later it will call 
registertoken(null)

after that i am getting a token , it will hit a end point "subscribe" later i 
will get a token  in Json form.  how do i wait for token generate and it will 
return a

promise will resolved state. now  i am getting a promise with "pending" state 
and its values is undefined.



workaround for the above problem:


$timeout(function(){
  Notification.getFirebaseToken().then(function(res) {
    $scope.token = res;
    console.log('CALLED', res);
    myService.registerToken({"token":[$scope.token]})
      .then(function (response) {
      });
  });
},1000)


Same thing I tried with above code . User Logs in later he all get window for 
"subscription" permission after allowing i am getting a token later it will call

registerToken(token) and able to register that token to the database . now the 
question is  here  the token generating end point "subscribe" calling two times 
why??

 is this correct way doing ?? suggest me other ways without  using the time out 
function.



-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to