2015-02-07 95 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發送消息,請在調試選項卡上選擇消息類型自定義模板