cordova angularjs强制app暂停等待用户权限

问题描述 投票:0回答:2

我使用的是cordova-firebase-plugin,推送通知的iOS要求之一是授予权限,问题是使用cordova-firebase-plugin grantPermission没有成功/错误的正确回调 - 因此当调用grantPermission时它将权限请求弹出给用户,但在后台应用程序继续加载。

插件权限调用是一个没有回调的基本功能:

window.FirebasePlugin.grantPermission();

我需要暂停应用加载,并且只有在用户授予/拒绝权限请求后才能继续。以下是我尝试在我的应用的app init部分执行此操作:

  function iosPush() {
    var q = $q.defer() ;
    if (/(iPad|iPhone|iPod)/i.test(navigator.userAgent)) {
      window.FirebasePlugin.grantPermission(function(status) {
        q.resolve(status) ;
      },function(err) {errMgmt("ctrl/init",35,"iOS Push ask Permission error: "+err) });) ;
    } else {
      q.resolve("Android") ;
    }
    return q.promise ;
  }

    iosPush().then(function(status) {
      return getLocationAuth()
    }).then(function(status) {
      ...do other stuff...
    }) ;

我暂停应用程序的尝试不起作用。有人可以协助或指出我如何在请求iOS权限时实现app init暂停?

最后,无论用户选择什么,授予或拒绝许可,status始终是null

ios angularjs cordova callback user-permissions
2个回答
0
投票

我遇到过同样的问题。

我最终使用https://github.com/dpa99c/cordova-diagnostic-plugin来检查许可状态。弹出一个带按钮的弹出窗口。当用户关闭权限对话框时,他必须通过单击按钮来关闭弹出窗口。然后我再次检查权限状态。这不是一个解决方案,它只是一种解决方法。有点难看,但有效。

你自己找到了解决方案吗?


0
投票

我原来的问题很接近......不得不将其修改为以下内容:

  function iosPush() {
    var q = $q.defer() ;
    if (/(iPad|iPhone|iPod)/i.test(navigator.userAgent)) {  // 1st hasPerm
      window.FirebasePlugin.hasPermission(function(data){
        if (data.isEnabled == false) {
          window.FirebasePlugin.grantPermission(function(status) {  // if no, grant
            // Permission Granted or notGranted...need to check again.
            window.FirebasePlugin.hasPermission(function(data){  
            // 2nd hasPerm, if changed, set internal db 
              var oldPushEnabled = getDB('dev_pushEnabled') ;
              if (data.isEnabled == true) { 
                var pushIsEnabled = 1 ; 
              } else {
                var pushIsEnabled = 0 ;
              }
              if (oldPushEnabled != pushIsEnabled) {
                setDB('dev_pushEnabled',pushIsEnabled) ; // set local app db value
                q.resolve("PushStatusNotChanged") ;  // push enable status has changed
              } else {
                q.resolve("PushStatusChanged") ;  // push enable status has not changed
              }
            }) ;  // close 1st hasPermission
          },function(error) {
            // Permission NOT GRANTED
            q.resolve("PushNotGranted") ;
          }) ; // grantPermission
        } else {
          q.resolve("PushGranted") ;  // authorization was previously granted
        }
      }) ;  // close 2nd hasPermission
    } else {
      q.resolve("Android") ;
    }
    return q.promise ;
  }

  iosPush().then(function(status) {
     return getLocationAuth()
  }).then(function(status) {
    ...do other stuff...
  }) ;

在函数中,double hasPermission充当grantPermission成功/失败回调。有点像kludge,但它的功效就像魅力。

© www.soinside.com 2019 - 2024. All rights reserved.