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

Push Notification using GCM for andorid in ionic full example

本文关键字:andorid 进行 GCM 通知      更新时间:2023-09-26

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

这是我的代码。

文件名为应用程序.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>

检查这个ng-cordova推送通知插件

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

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

使用离子框架从设备检索设备令牌

在此处查看完整的演示博客,并有明确的解释https://devdactic.com/ionic-push-notifications/