在离子完整示例中使用GCM推送通知进行andorid

时间:2016-07-14 06:59:36

标签: javascript android ionic-framework cordova-plugins

如何在IONIC中开发Android的推送通知? 请给我一些工作示例代码。

这是我的代码。

文件名是app.js

 .config(['$ionicAppProvider', function($ionicAppProvider)
 {
   $ionicAppProvider.identify({
                        app_id: 'APP_ID,
                        api_key: 'PUBLIC_KEY'
                        });
  }])

.controller('PushCtrl', function($scope, $rootScope, $ionicUser, $ionicPush)
{ 
 $rootScope.$on('$cordovaPush:tokenReceived', function(event, data)
 {  
   alert("Successfully registered token " + data.token);
   console.log('Ionic Push: Got token ', data.token, data.platform);   
   $scope.token = data.token;});

   $scope.identifyUser = function()
   {
      var user = $ionicUser.get();
      if(!user.user_id)
      {
        // Set your user_id here, or generate a random one.
        user.user_id = $ionicUser.generateGUID();
      };

// Metadata
angular.extend(user, {
    name: 'Simon',
    bio: 'Author of Devdactic'
});

  // Identify your user with the Ionic User Service
  $ionicUser.identify(user).then(function()
  {
      $scope.identified = true;
      console.log('Identified user'+user.name+'\n ID'+user.user_id);
   });
}; 

//为推送通知注册设备

$scope.pushRegister = function()
{
  console.log('Ionic Push: Registering user');
 // Register with the Ionic Push service.  All parameters are optional.
 $ionicPush.register({
  canShowAlert: true, //Can pushes show an alert on your screen?
  canSetBadge: true, //Can pushes update app icon badges?
  canPlaySound: true, //Can notifications play a sound?
  canRunActionsOnWake: true, //Can run actions outside the app,
  onNotification: function(notification)
 {
    return true;
 }
});
};
});
你能告诉我哪里错了吗?我在这一行得到了错误

 <script src="lib/angular-websocket/angular-websocket.js"></script>

1 个答案:

答案 0 :(得分:0)

检查此ng-cordova推送通知插件

http://ngcordova.com/docs/plugins/pushNotifications/

检查我的堆栈答案如何获取设备令牌

Retrieve device token from device, using ionic framework

点击这里查看完整的演示博客,并附上清晰的解释 https://devdactic.com/ionic-push-notifications/