服务器未接收GCM的注册ID

时间:2016-07-07 05:48:38

标签: android angularjs ionic-framework google-cloud-messaging

我有离子应用程序从服务器接收推送通知。以下是我的app.js

angular.module('starter', ['ionic'])

.run(function($ionicPlatform,RequestsService,$rootScope) {

document.addEventListener("deviceready",onDeviceReady,false);
$rootScope.device_token;
function onDeviceReady(){
    alert("device ready");
    var push = PushNotification.init({ "android": {"senderID": "600956580762"},
                "ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
    push.on('registration', function(data) {
        alert(data.registrationId);
        device_token = data.registrationId;
    });

    push.on('notification', function(data) {
      alert(data.title+" Message: " +data.message);
    });

     push.on('error', function(e) {
          alert("error "+e.message);
     });
}
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

  });
})

以下是我的RequestsService,js,

(function(){

    angular.module('starter')
    .service('RequestsService', ['$http', '$q', '$ionicLoading','$ionicPopup','$rootScope',  RequestsService]);

    function RequestsService($http, $q, $ionicLoading,$ionicPopup,$rootScope){
        var base_url = 'http://localhost:3000';
        function register(device_token){

            var deferred = $q.defer();
            $ionicLoading.show();

            $http.post(base_url + '/register', {'device_token':device_token})
                .success(function(response){
                    $ionicLoading.hide();
                    deferred.resolve(response);

                })
                .error(function(data){
                    deferred.reject();
                });


            return deferred.promise;

        };


        return {
            register: register
        };
    }
})();

我成功收到GCM的注册ID。但是当我运行服务器时,它没有从GCM接收注册ID。下面是server.js,

var express = require('express');
var gcm = require('node-gcm');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var server = app.listen(3000, function(){

 console.log('server is running');

});
app.use(function(req, res, next){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
var device_token;
app.post('/register', function(req, res){
    device_token = req.body.device_token;
    console.log('device token received');
    console.log(device_token);
    /* TODO: save the device_token into your database*/
    res.send('ok');
});
app.get('/push', function(req, res){

    var device_tokens = []; //create array for storing device tokens
    var retry_times = 4; //the number of times to retry sending the message if it fails

    var sender = new gcm.Sender('AIzaSyB0tUA1NSJG32BYV21hntiPAFEIVIRiuhs'); //create a new sender
    var message = new gcm.Message(); //create a new message

    message.addData('title', 'New Message');
    message.addData('message', 'Hello this is a push notification');
    message.addData('sound', 'notification');

    message.collapseKey = 'testing'; //grouping messages
    message.delayWhileIdle = true; //delay sending while receiving device is offline
    message.timeToLive = 3; //the number of seconds to keep the message on the server if the device is offline

    /*
    YOUR TODO: add code for fetching device_token from the database
    */

    device_tokens.push(device_token);

    sender.send(message, device_tokens, retry_times, function(result){
        console.log(result);
        console.log('push sent to: ' + device_token);
    });

    res.send('ok '+device_token);
});

我的网址是baseurl是localhost:3000。我无法理解为什么server_token在服务器中未定义。

0 个答案:

没有答案