2017-08-01 88 views
0

我对科尔多瓦很新,也使用城市飞艇推送通知概念。我有一个要求,就像我必须在前台获取推送通知一样。但我只在后台收到通知。我没有在前台收到通知。
这里是我的代码:城市飞艇推送通知不显示在前景在amdroid

function initPush(){ 
     //alert("Push initialize"); 
     console.log("Device ready!"); 
     var status = localStorage.getItem("pushEnable") 
     if(status == null){ 
      UAirship.setUserNotificationsEnabled(true); 
     }else{ 
       if (localStorage.getItem("pushEnable") == "true") { 
       // alert("SET USER NOTFICAION ENABLE "); 
       UAirship.setUserNotificationsEnabled(true); 
      } else { 
       // alert("SET USER NOTFICAION DISBALE "); 
       UAirship.setUserNotificationsEnabled(false) 
      } 
     } 
     localStorage.setItem("uuid", device.uuid); 
     //alert("DEVICE ID ==>"+device.uuid); 
     var onRegistration = function(event) { 
     // alert("ON REGISTRAION CALLED"); 
     window.plugins.spinnerDialog.show(null,"loading....", true); 
      if (!event.error) { 
      window.plugins.spinnerDialog.hide(); 
      // alert("ON REGISTRAION SUCESS"); 
       console.log("Reg Success:---> " + event.channelID) 
      // alert("ON REGISTRAION EVENT "+ JSON.stringify(event)); 
       var uuid = localStorage.getItem("uuid") 
       localStorage.setItem("channelId", event.channelID) 
       var deviceInfo = { 
        channel_id: event.channelID, 
        device_id: uuid 
       }; 
       UAirship.setNamedUser(uuid, function() { 
        //setNamedUser(uuid) 
        //alert("SETNAME SUCESS"); 
       }) 
      }else{ 
      window.plugins.spinnerDialog.hide(); 
      // alert("ON REGISTRAION FAILED"); 
      } 
     } 
     //alert(device.serial) 
     var onPushReceived = function(event) { 
     alert("ON PUSH RECEIVED CALL"); 
      if (event.message) { 
       dialogAlert("Message From LockerRoom", event.message) 
       alert("Message From LockerRoom", event.message) 
       console.log("Received push: " + event.message) 
      } else { 
       console.log("No incoming message") 
      } 
     } 
     // Notification opened callback 
     var notificationOpened = function(event) { 
      if (event.message) { 
       console.log("Notification opened: " + event.message) 
      } else { 
       console.log("No incoming message") 
      } 
     } 
     // Deep link callback 
     var handleDeepLink = function(event) { 
      console.log("Deep link: " + event.deepLink) 
     } 
     // Register for any urban airship events 
     document.addEventListener("urbanairship.registration", onRegistration, false) 
     document.addEventListener("urbanairship.push", onPushReceived, false) 
     document.addEventListener("urbanairship.notification_opened", notificationOpened, false) 
     document.addEventListener("urbanairship.deep_link", handleDeepLink, false) 
     // Handle resume 
     document.addEventListener("resume", function() { 
     // alert("Device resume!") 
      UAirship.resetBadge() 
      // Reregister for urbanairship events if they were removed in pause event 
      document.addEventListener("urbanairship.registration", onRegistration, false) 
      document.addEventListener("urbanairship.push", onPushReceived, false) 
     }, false) 
     // Handle pause 
     document.addEventListener("pause", function() { 
     // alert("Device Pause!") 
      // Remove urbanairship events. Important on android to not receive push in the background. 
      document.removeEventListener("urbanairship.registration", onRegistration, false) 
      document.removeEventListener("urbanairship.push", onPushReceived, false) 
     }, false) 
     // Get the launch notification if its available. 
     //this.receivedEvent('deviceready'); 
} 
    function initiateUI() { 
     //alert("InitUI"); 
     var struuid = localStorage.getItem("uuid") 
     $('#namedUser').text(struuid) 
     var setNamedUser = function(namedUser) { 
      document.getElementById("setNamedUserField").disabled = true; 
      var namedUser = $("#namedUser").val(struuid) 
      UAirship.setNamedUser(namedUser, function() { 
           setNamedUser(struuid) 
           }) 
      $("#namedUser").text(namedUser) 
     } 
     // Vibrate and Sound is only available on Android 
     if (device.platform != "Android") { 
      $("#soundEnabledSection").hide() 
      $("#vibrateEnabledSection").hide() 
     } 
     UAirship.getNamedUser(function(namedUser) { 
          //alert("getNamedUser--->" + getNamedUser); 
          if (namedUser) { 
          console.log("Got namedUser: " + namedUser) 
          setNamedUser(namedUser) 
          } 
          }) 
     // Update the interface with the current UA settings 
     //var isEnabled = localStorage.getItem("pushEnable"); 
     UAirship.isUserNotificationsEnabled(function(isEnabled) { 
             // alert("isUserNotificationsEnabled **(* ---- ENTER " + isEnabled); //0 - false 
              if (localStorage.getItem("pushEnable") == null) { 
              $('#pushEnabled').val(isEnabled ? 'on' : 'off').change() 
              } else if (localStorage.getItem("pushEnable") == "true") { 
              $('#pushEnabled').val('on').change(); 
     } else { 
              $('#pushEnabled').val('off').change(); 
     } 
     }) 
     // Set up change callbacks for the UI elements 
     $('#pushEnabled').change(function() { 
     var isEnabled = ($('#pushEnabled').val() == "on") 
     UAirship.setUserNotificationsEnabled(isEnabled) //TRUE FALSE 
     localStorage.setItem("pushEnable", isEnabled); // TRUE FALSE 
    }) 
} 

我不知道我在这里做了什么错误。任何人都可以请帮我解决这个问题。 在此先感谢。

回答

0

默认情况下,android的推送通知会在应用程序处于前景时发出警报。将直接调用onPushReceived事件处理程序。您可能只会收到一个您调用的对话框警报,并将打印控制台消息。

如果您希望它在发送通知时显示为通知,请使用"forceShow":"true"值。来源this。 (我不确定它是否适用于urbanairship插件,但请尝试让我知道)。

无论如何对于科尔多瓦其简化。您可以使用phonegap-push-plugin。见示例here