当前位置: 首页 > 知识库问答 >
问题:

Ionic页面转换不适用于具有推送功能的页面

翟光赫
2023-03-14

我在我的应用程序中实现了android推送接收,只要应用程序在该页面启动,它就可以正常工作。但是如果您导航到实现推送通知的消息页,则转换不起作用。

在没有push实现的情况下,转换运行良好。

我想知道是否有人在实现特性时遇到过类似的问题。

.controller('BuzonMenuCtrl', function($scope, $window, $state, $ionicPlatform) {
  $scope.botonBuzon = function(){
    $state.go('buzon');
  };
})

.controller('BuzonCtrl', function($scope, $rootScope, $window, $ionicActionSheet, $ionicPopup, $state, $http, dataFactory, pushFactory) {

  // Activacion de la funcionalidad push
  pushFactory.funcionalidadPush();

}

工厂。js公司

.factory('pushFactory', ['$rootScope','$http','$state','$ionicLoading','$ionicPlatform','$cordovaPush','dataFactory',
                          function($rootScope,$http,$state,$ionicLoading,$ionicPlatform,$cordovaPush,dataFactory) {

  /* Objeto del factory */
  var fac = {};

  fac.funcionalidadPush = function(){
    if (ionic.Platform.isAndroid()){
      var androidConfig = {
        "senderID": "94XXXXXXXXXX",
        "ecb": "casosPush"
      };

      $rootScope.data.pushplatform = "gcm";
      alert('Entro en modo Android');

    };

    if (ionic.Platform.isIOS()){
      alert('Entro en modo iOS');
    };

    $ionicPlatform.ready(function() {
      $cordovaPush.register(androidConfig).then(function(result) {
        // Success
      }, function(err) {
        // Error
      })

      window.casosPush = function (notification) {
        switch(notification.event) {
          case 'registered':
            if (notification.regid.length > 0 ) {
              alert('registration ID = ' + notification.regid);
              $rootScope.data.token = notification.regid;

            }
            break;

          case 'message':
            // this is the actual push notification. its format depends on the data model from the push server
            //alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
            $rootScope.mensajes.push(notification);
            break;

          case 'error':
            alert('GCM error = ' + notification.msg);
            break;

          default:
            alert('An unknown GCM event has occurred');
            break;
        }
      };

      // WARNING: dangerous to unregister (results in loss of tokenID)
      $cordovaPush.unregister(options).then(function(result) {
        // Success!
      }, function(err) {
              // Error
      })

    }, false);
  };


  return fac;

}]);

一个pp.js

angular.module('notPush', ['ionic', 'ngCordova', 'notPush.controllers'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider

  .state('splash', {
    url: '/splash',
    templateUrl: 'templates/splash.html',
    controller: 'SplashCtrl'
  })

  .state('buzonMenu', {
    url: '/buzonMenu',
    templateUrl: 'templates/buzonMenu.html',
    controller: 'BuzonMenuCtrl'
  })

  .state('buzon', {
    url: '/buzon',
    templateUrl: 'templates/buzon.html',
    controller: 'BuzonCtrl'
  })

  // If none of the above states are matched, use this as the fallback:
  $urlRouterProvider.otherwise('/buzonMenu');

});

编辑

正如我尝试过的评论中所建议的那样

$window.location.reload(true);

除了让屏幕闪烁之外什么也不做,而且

$state.go($state.current, {}, {reload: true});

它尝试重新加载页面,但仅在损坏状态下,它加载白色背景和按钮(没有颜色),但没有其他内容。

编辑2一些澄清:

  1. 推送通知的代码写在buzon_page控制器中,这是有问题的页面。
  2. 当我说“过渡不起作用”时,我的意思是页面没有加载,但该页面的所有警报仍然被触发,推送令牌仍然被检索。是一个视觉问题,视图没有加载。
  3. 问题与HTTP调用无关,因为它们当前被注释。

编辑3

我将通知代码移动到工厂,而不是将其放在控制器中(我想应该是这样),但没有任何改进。

我在问题期间添加了adb logcat的结果。有两件事值得我注意:

>

1219             AudioTrack  W  AUDIO_OUTPUT_FLAG_FAST denied by client   
2531             PushPlugin  V  execute: action=register
2531             PushPlugin  V  execute: data=[{"senderID":ID DELETED FOR PRIVACY ISSUES,"ecb":"casosPush"}]
2531             PushPlugin  V  execute: jo={"senderID":ID DELETED FOR PRIVACY ISSUES,"ecb":"casosPush"}
2531             PushPlugin  V  execute: ECB=casosPush senderID=ID DELETED FOR PRIVACY ISSUES
2531           GCMRegistrar  D  resetting backoff for ID DELETED FOR PRIVACY ISSUES
1219   InputMethodManager..  W  Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@df31fbd attribute=null, token = a
                               ndroid.os.BinderProxy@28f2dc02
2531           GCMRegistrar  V  Registering app ID DELETED FOR PRIVACY ISSUES of senders ID DELETED FOR PRIVACY ISSUES
2531   SystemWebChromeCli..  D  file:///android_asset/www/lib/ionic/js/ionic.bundle.js: Line 20243 : ReferenceError: options is not defined
2531   SystemWebChromeCli..  D  at file:///android_asset/www/js/factorys.js:214:31
2531   SystemWebChromeCli..  D  at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:44687:19
2531   SystemWebChromeCli..  D  at Object.ionic.Platform.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:2120:9)
2531   SystemWebChromeCli..  D  at Object.self.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:44685:26)
2531   SystemWebChromeCli..  D  at Object.fac.funcionalidadPush (file:///android_asset/www/js/factorys.js:166:20)
2531   SystemWebChromeCli..  D  at new <anonymous> (file:///android_asset/www/js/controllers.js:440:15)
2531   SystemWebChromeCli..  D  at invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12821:17)
2531   SystemWebChromeCli..  D  at Object.instantiate (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12829:27)
2531   SystemWebChromeCli..  D  at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:17098:28
2531   SystemWebChromeCli..  D  at self.appendViewElement (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48110:24)
2531               chromium  I  [INFO:CONSOLE(20243)] "ReferenceError: options is not defined
2531               chromium  I  at file:///android_asset/www/js/factorys.js:214:31
2531               chromium  I  at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:44687:19
2531               chromium  I  at Object.ionic.Platform.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:2120:9)
2531               chromium  I  at Object.self.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:44685:26)
2531               chromium  I  at Object.fac.funcionalidadPush (file:///android_asset/www/js/factorys.js:166:20)
2531               chromium  I  at new <anonymous> (file:///android_asset/www/js/controllers.js:440:15)
2531               chromium  I  at invoke (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12821:17)
2531               chromium  I  at Object.instantiate (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12829:27)
2531               chromium  I  at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:17098:28
2531               chromium  I  at self.appendViewElement (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48110:24)", source: file:///android_asset/www/lib/ionic
                               /js/ionic.bundle.js (20243)
1604                    GCM  D  GcmService start Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gms cmp=com.google.android.gms/.gcm.GcmServi
                               ce (has extras) } com.google.android.c2dm.intent.REGISTER
2531   GCMBroadcastReceiver  V  onReceive: com.google.android.c2dm.intent.REGISTRATION
2531           GCMRegistrar  V  Setting the name of retry receiver class to com.plugin.gcm.CordovaGCMBroadcastReceiver
2531   GCMBroadcastReceiver  V  GCM IntentService class: com.plugin.gcm.GCMIntentService
2531   GCMBaseIntentService  V  Acquiring wakelock
2531   GCMBaseIntentService  V  Intent service name: GCMIntentService-GCMIntentService-1
2531           GCMRegistrar  V  Registering receiver
2531   GCMBaseIntentService  D  handleRegistration: registrationId = TOKEN DELETED FOR PRIVACY ISSUES, error = null, unregistered = null
2531           GCMRegistrar  D  resetting backoff for ID DELETED FOR PRIVACY ISSUES
2531           GCMRegistrar  V  Saving regId on app version 10
2531       GCMIntentService  V  onRegistered: TOKEN DELETED FOR PRIVACY ISSUES
2531       GCMIntentService  V  onRegistered: {"event":"registered","regid":TOKEN DELETED FOR PRIVACY ISSUES}
2531             PushPlugin  V  sendJavascript: javascript:casosPush({"event":"registered","regid":TOKEN DELETED FOR PRIVACY ISSUES})
2531   GCMBaseIntentService  V  Releasing wakelock
2531          EGL_emulation  W  eglSurfaceAttrib not implemented
2531         OpenGLRenderer  W  Failed to set EGL_SWAP_BEHAVIOR on surface 0xa4cdd7e0, error=EGL_SUCCESS
1219             AudioTrack  W  AUDIO_OUTPUT_FLAG_FAST denied by client
1219   InputMethodManager..  W  Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@35215d0a attribute=null, token = 
                               android.os.BinderProxy@28f2dc02

共有3个答案

段干庆
2023-03-14

当我试图在视图转换的同时进行REST调用时,我的应用程序中也出现了类似的问题。这是因为$http promise正在中断视图渲染。如果将$http调用包装在$timeout中,则可以解决此问题,因为未提及时间的$timeout会将$http放入队列中,而不会中断当前任务。

你可以做一些像

$scope.$on('$cordovaPush:notificationReceived', handleNotification);

handleNotification = function(event, notification){
    $timeout(function(event, notification) {
            switch(notification.event) {
                case 'registered':
                    if (notification.regid.length > 0 ) {
                        alert('registration ID = ' + notification.regid);
                        $scope.regid = notification.regid;
                        var user = { user: 'David', type: 'android', token: notification.regid };
                        $http.post('http://172.16.16.101:8000/tokens', JSON.stringify(user));
                    }
                    break;

                case 'message':
                    // this is the actual push notification. its format depends on the data model from the push server
                    //alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                    alert(JSON.stringify([notification]));
                    var aux = {title:'',message:'',payload: { valor1:true }}
                    $scope.mensajes.push(notification);
                    break;

                case 'error':
                    alert('GCM error = ' + notification.msg);
                    break;

                default:
                    alert('An unknown GCM event has occurred');
                    break;
            }
        });
}

这是一个粗略的想法,您必须修改和完善以满足您的需要

陈兴朝
2023-03-14

来自一个正在工作的Ionic项目的示例代码。作为如何初始化PushPlugin的示例。

var exapp = angular.module('exapp',
                  ['ionic',
                   'ui.select2',
                   'exapp.controllers',
                   'exapp.services']);

exapp.run(function($ionicPlatform, $state, Notifications, geo) {
    $ionicPlatform.ready(function() {
    try{
        var pushNotification = window.plugins.pushNotification;
    } catch (ex){

    }
    var successfn = function(result){
        // window.alert("S: " + result);
    };
    var errorfn   = function(result){
        // window.alert("E: " + result);
    };
    window.onCB = function(e){
        switch (e.event){
        case 'registered':
        if (e.regid.length > 0){
            localStorage.setItem('registration_id', e.regid);
        }
        break;
        case 'message':
        if (e.foreground){
            navigator.notification.beep(1);
        }
        $state.go('app.notifications');
        break;
        }
    };
    try{
        pushNotification.register(
        successfn,
        errorfn,
        {
            "senderID": "191919191919191",
            "ecb"     : "window.onCB"
        }
        );
    } catch(e){

    }
    });
});

// States
exapp.config(function($stateProvider, $urlRouterProvider) {
    $stateProvider

    .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "templates/menu.html",
        controller: 'AppCtrl'
    })

    .state('app.profile', {
        url: "/profile",
        views: {
        'menuContent': {
            templateUrl: "templates/profile.html",
            controller: "ProfileCtrl"
        }
        }
    })

    .state('app.login', {
        url: "/login",
        views: {
        'menuContent' :{
            templateUrl: "templates/login.html",
            controller: 'AuthCtrl'
        }
        }
    });

    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/app/profile');
});

此处提供,以防无法获得要点

夹谷成仁
2023-03-14

多亏了评论中的帮助,我解决了这个问题,所以我将在这里写下解决方案。

评论建议使用此代码,以便在应用程序启动时立即激活推送通知。

我将在这里添加我自己的代码,以防万一,警报和$rootScope变量用于测试目的。

/* 
   app.js
*/
angular.module('notPush', ['ionic', 'notPush.controllers', 'notPush.factorys'])

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

    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }

    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

    $rootScope.mensajes = [];

    // Push code
    try{
      var pushNotification = window.plugins.pushNotification;
    } catch (ex){

    }
    var successfn = function(result){
      alert("Success: " + result);
    };
    var errorfn   = function(result){
      window.alert("Error: " + result);
    };
    window.casosPush = function(notification){
      switch (notification.event){
        case 'registered':
          if (notification.regid.length > 0){
            alert('registration ID = ' + notification.regid);
          }
          break;

        case 'message':
          alert(JSON.stringify([notification]));
          $rootScope.mensajes.push(notification);
          break;

        case 'error':
          alert('GCM error = ' + notification.msg);
          break;

        default:
          alert('An unknown GCM event has occurred');
          break;
      }
    };
    try{
      pushNotification.register(
        successfn,
        errorfn,
        {
          "senderID": "94XXXXXXXXXX",
          "ecb"     : "window.casosPush"
        }
      );
    } catch(notification){

    } 
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider

  .state('splash', {
    url: '/splash',
    templateUrl: 'templates/splash.html',
    controller: 'SplashCtrl'
  })

  .state('registro', {
    url: '/registro',
    templateUrl: 'templates/registro.html',
    controller: 'RegistroCtrl'
  })

  .state('buzonMenu', {
    url: '/buzonMenu',
    templateUrl: 'templates/buzonMenu.html',
    controller: 'BuzonMenuCtrl'
  })

  .state('buzon', {
    url: '/buzon',
    templateUrl: 'templates/buzon.html',
    controller: 'BuzonCtrl'
  })

  .state('detallesSimple', {
    url: '/detallesSimple',
    templateUrl: 'templates/detallesSimple.html',
    controller: 'DetallesCtrl'
  })

  .state('detallesDoble', {
    url: '/detallesDoble',
    templateUrl: 'templates/detallesDoble.html',
    controller: 'DetallesCtrl'
  })

  .state('detallesWV', {
    url: '/detallesWV',
    templateUrl: 'templates/detallesWV.html',
    controller: 'DetallesWVCtrl'
  })

  // If none of the above states are matched, use this as the fallback:
  $urlRouterProvider.otherwise('/splash');

});
 类似资料:
  • 我对我的可视寻呼机有一个自定义要求。我想要的是我的寻呼机应该有一个类似DepthPageTransformer的默认水平pageTransformer。现在,在屏幕上我有一个按钮,我希望我的当前页面从底部滑动到顶部,我的下一个页面像VerticalPageTransformer一样替换它,一旦页面改变,页面转换器应该改变回默认的DepthPageTransformer。 所以基本上我想在运行时应用

  • 我想使用和,有什么方法或可靠的解决办法可以使Pageable与jpaQuery一起工作吗?

  • 我正在使用Ionic构建一个应用程序,但自从ios 9更新以来,我的代码每次都会被破坏。 这是我的代码,当用户单击“注销”时,它会启动一个函数,更改状态,然后刷新页面。 但现在我注意到我的$状态。go是一个异步和$窗口。地方重载()在转换完成之前启动。有什么方法可以在重新加载页面之前等待转换完成?

  • 我使用的是Spring数据JPA。 我的控制器如下所示 现在我决定发送一个PageDTO对象来代替Page对象,以限制发送内容。有什么办法可以用java 8把页面转换成页面? 我看到Page是从派生而来的,所以我想我可以做一些类似的事情,但不知道如何将它与PageDTO和UserDTO结合起来。 有没有有效的java 8方法来做到这一点。 我想出了这个解决方案 我想知道是否有另一种有效的方法来做到

  • 问题内容: 我通过AJAX和WordPress分页加载了一些帖子,并使用以下函数来计算分页: 问题是通过AJAX创建了分页,因此使此链接看起来像: 但是,我想要达到的实际URL就是为此: 有没有办法在AJAX上使用此功能,并且仍然获得正确的分页URL? 问题答案: 我可以为您想到三个选择: 编写自己的版本将允许您指定基本URL 在调用时覆盖变量 要调用该函数,请返回整个分页的HTML,然后使用JS

  • 我正在编写一个 C# 应用程序来解码此字符串: "--W 3原文3 R hcn Rfd Gl tZ SI 6 I jAi L CJ w cm 9 k dW N 0 X 2 lk I jo iO D Q wM DMz MD气LC J 1 cm wi O iI if Sx 7 InN 0 Y XJ 0 X 3 Rp b WU iO iI 3 OS In by b 2 R 1 Y 3 Rfa WQ iO