Urban Airship推送通知不在amdroid的Foreground中显示

时间:2017-08-01 10:02:22

标签: android cordova urbanairship.com

我是Cordova的新手,我也使用Urban Airship Push通知概念。我有一个要求,就像我必须在前台获得推送通知。但我只在后台收到通知。我没有在前台收到通知 这是我的代码:

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
  })
}

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

1 个答案:

答案 0 :(得分:0)

默认情况下,Android推送通知会在应用程序处于前台时显示为警报。将直接调用onPushReceived事件处理程序。您可能只收到一个已拨打的Dialog警报,并且将打印控制台消息。

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

无论如何,对于cordova它的简化。您可以使用phonegap-push-plugin。请参见示例here