2015-02-07 51 views
1

这个问题很长并涉及到,但我希望有人能帮我一把。我正在编写需要发送和接收推送通知的Phonegap应用程序。我想为此使用Azure服务总线,以便我可以部署到多个移动平台。现在我只是测试Android。我设置了我的Google和Azure帐户,并且其代码正常工作。当我在我的设备上部署我的应用时,onGcmNotification被解雇,我从google获得regid。然后我注册了提供的regid和模板,之后我得到了一个提醒(“注册了Hub!”),所以我认为一切正常。但是,当通过Azure调试控制台进行测试时,我无法获得任何推送通知。见下图。它只是一直告诉我“未找到所选平台的注册”。我的已部署应用程序中没有弹出任何内容,onGcmNotification永远不会被触发。我错过了什么?是否有另一种方法来完成我正在尝试的目标?Windows Azure服务总线和GCM推送通知

Azure Push Notification Testing Screen

var GCM_SENDER_ID = 'XXXXXXXXXXXXX'; 
    var MOBILE_SERVICE_URL = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; 
    var MOBILE_SERVICE_APP_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; 
    var mobileServiceClient; 
    var pushNotification; 

    var app = { 
     initialize: function() { 
      this.bindEvents(); 
     }, 
     bindEvents: function() { 
      document.addEventListener('deviceready', this.onDeviceReady, true); 
     }, 

     onDeviceReady: function() { 
      angular.element(document).ready(function() { 

       mobileServiceClient = new WindowsAzure.MobileServiceClient(MOBILE_SERVICE_URL, MOBILE_SERVICE_APP_KEY); 

       // Create a pushNotification (from the PushPlugin). 
       pushNotification = window.plugins.pushNotification; 

       // Platform-specific registrations. 
       if (device.platform == 'android' || device.platform == 'Android') { 
        // Register with GCM for Android apps. 
        pushNotification.register(successHandler, errorHandler, 
         { 
          "senderID": GCM_SENDER_ID, 
          "ecb": "onGcmNotification" 
         }); 
       } else if (device.platform === 'iOS') { 
        // Register with APNS for iOS apps. 
        pushNotification.register(tokenHandler, errorHandler, 
         { 
          "badge": "false", 
          "sound": "false", 
          "alert": "true", 
          "ecb": "onApnsNotification" 
         }); 
       } 

       angular.bootstrap(document, ['app']); 
      }); 
     }, 
    }; 


    // Handle a GCM notification. 
    function onGcmNotification(e) { 
     switch (e.event) { 
      case 'registered': 
       // Handle the registration. 
       if (e.regid.length > 0) { 
        console.log("gcm id " + e.regid); 

        if (mobileServiceClient) { 

         // Create the integrated Notification Hub client. 
         var hub = new NotificationHub(mobileServiceClient); 

         // Template registration. 
         var template = "{ \"data\" : {\"message\":\"$(message)\"}}"; 

         // Register for notifications. 
         // (gcmRegId, ["tag1","tag2"], templateName, templateBody) 
         hub.gcm.register(e.regid, null, "myTemplate", template).done(function() { 
          alert("Registered with hub!"); 
         }).fail(function (error) { 
          alert("Failed registering with hub: " + error); 
         }); 
        } 
       } 
       break; 

      case 'message': 
       console.log("received message: " + e.message); 
       if (e.foreground) { 
        alert(e.payload.message); 
       } 
       break; 

      case 'error': 
       alert('GCM error: ' + e.message); 
       break; 

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

此博客文章可能有所帮助:http://blogs.msdn.com/b/azuremobile/archive/2014/06/17/push-notifications-to-phonegap-apps-using-notification-hubs-integration.aspx – 2015-02-18 07:51:35

+0

我正在经历相同的结果。你有没有得到任何结果? – ChepA 2015-07-01 14:45:02

回答

0

我遇到在Azure上相同的结果。尽管使用Android,推送通知仍在为我工作。第一次让我的移动服务发送通知成功插入后,我收到了通知。

在Azure门户中,您正在使用的移动服务转到数据 - >表 - >脚本 - >插入,然后使用此脚本在插入成功时发送推送通知。

function insert(item, user, request) { 
// Define a payload for the Google Cloud Messaging toast notification. 
var payload = { 
    data: { 
     message: '{ "message" : "New item added: ' + item.text + '" }'; 
    } 
};  
request.execute({ 
    success: function() { 
     // If the insert succeeds, send a notification. 
     push.gcm.send(null, payload, { 
      success: function(pushResponse) { 
       console.log("Sent push:", pushResponse, payload); 
       request.respond(); 
       },    
      error: function (pushResponse) { 
       console.log("Error Sending push:", pushResponse); 
       request.respond(500, { error: pushResponse }); 
       } 
      }); 
     }, 
    error: function(err) { 
     console.log("request.execute error", err) 
     request.respond(); 
    } 
    }); 
} 

现在,即使设备没有显示在Azure上的通知中心中,所有注册的设备也会在表中插入内容时收到通知。

您也可以从本地机器使用节点库发送通知:

https://github.com/ToothlessGear/node-gcm

我以前DevGirl's tutorial得到这个工作。让我知道这些事情是否有效。希望有助于!

PS。我使用与您的代码类似的代码推送iOS和Android的通知,但仍然无法从Azure的通知中心的调试选项卡发送测试通知。

0

我浪费了我的生活2天尝试通过Azure中发送邮件到我的移动应用程序使用通知中心,最后我做到了。 注意功能onGcmNotification,当您收到注册时注册一个自定义模板。因此,如果要使用Azure发送消息,请在调试选项卡上选择消息类型自定义模板