2

Eu tenho o seguinte json

{"message": "457896","additionalData":{"google.message_id":"0:149534266","coldstart":false,"collapse_key":"com.ionicframework.lucasteste693113","foreground":true}}

Como posso dar um alert para mostrar todas as informações ?

Estou querendo mostrar todas as informações eu tentei fazer assim:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        // esse nao veio 
        alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData.google.message_id));
        //{"message": "457896","additionalData":{"google.message_id":"0:149534266","coldstart":false,"collapse_key":"com.ionicframework.lucasteste693113","foreground":true}}
    });
})

e mas não funciono alguém poderia me ajudar?

2 Respostas 2

0

Na verdade já está funcionando, o problema é esse campo google.message_id, pois ele tem um ponto no meio. Faz assim:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        _not= JSON.parse(notification);
        alert("result: " + _not.message + _not.additionalData["google.message_id"]);
    });
}
3
  • Eu tentei de novo com JSON.stringify e sem ele com esse igual voce coloco, sem as aspas mas nao funciono. Não apareceu nenhuma mensagem. 21/05/2017 às 22:39
  • Fiz esse exemplo utilizando o próprio json que você postou: jsfiddle.net/joserogerio84/L51kpnr8 22/05/2017 às 23:10
  • Muito obrigado pela ajudar Eu consegui fazer desse jeito : alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData["google.message_id"])); E funciono no alert. 22/05/2017 às 23:19
0

Para mostrar todas as informações basta dar um 'stringify' no 'notification':

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        alert("notificação: " + JSON.stringify(notification));
    });
}

Editado: Tenta dar um parse antes:

.controller('principalController', function($http, $scope, $sce, $stateParams, $ionicScrollDelegate, $timeout, $rootScope, $cordovaPushV5) {
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, notification){
        _not= JSON.parse(notification);
        alert("result: " + _not.message + _not.additionalData.google.message_id);
    });
}
12
  • Eu estou querendo mostrar todas as informações separadas colocar todos os valores separados exemplo : alert("result: " + JSON.stringify(notification.message) + JSON.stringify(notification.additionalData.google.message_id)); Mas quando eu coloco additionalData.google.message_id eu nao estou conseguindo pegar o valor que esta vindo do meu json 21/05/2017 às 19:34
  • voce entendeu a minha duvida ? @joserogerio84 21/05/2017 às 19:34
  • Entendi, o que sai quando você executa: alert("notificação: " + JSON.stringify(notification)); ?? 21/05/2017 às 21:05
  • notificação : {"message": "457896","additionalData":{"google.message_id":"0:149534266","coldstart":false,"collapse_key":"com.ionicframework.lucasteste693113","foreground":true}} 21/05/2017 às 21:08
  • eu consegui pegar o message fazendo desse jeito : alert("notificação: " + JSON.stringify(notification.message)); Mas nao estou conseguindo pegar o resto. 21/05/2017 às 21:10

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .