1

我目前正在爲Xamarin for Android編寫移動應用程序,一旦我的公司購買Mac,我將開始添加iOS,以便開始開發iOS部分。我目前正在嘗試爲Azure通知中心編寫一個.Net移動服務後端,這將允許我從後端註冊設備,並向特定用戶和/或所有用戶發送推送通知。。用於通知中心的.Net移動服務後端

我一直遵循Azure文檔Getting Started With Notification Hub,並已成功執行單個平臺推送。超越這個例子是我陷入困境的地方。除此之外的每一個例子都完全沒有Android支持,只關注Windows Phone和iOS。我已經觀看了一些有關該主題的頻道9視頻,並且再次顯示了所有基於Windows Phone,Windows 8和iOS的視頻。

有沒有人有Azure通知中心的.Net移動服務後端的示例,它會將設備從後端註冊到通知中心?感謝您的時間。

回答

1

我還沒有GitHub上的示例代碼,但這裏有一個如何讓NotificationHub在Android上工作的要點。

using Microsoft.ServiceBus.Notifications; 
using Newtonsoft.Json; 

public class AndroidNotificationHub 
{ 
    private readonly NotificationHubClient _hubClient; 

    public AndroidNotificationHub() 
    { 
     const string cn = "YourConnectionStringHere"; 
     const string hubPath = "YourHubPathHere"; 
     _hubClient = NotificationHubClient.CreateClientFromConnectionString(cn, hubPath); 
    } 

    public async Task<RegistrationDescription> Register(string platform, string installationId, string registrationId, string userName) 
    { 
     // Get registrations for the current installation ID. 
     var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(installationId, 100); 

     var updated = false; 
     var firstRegistration = true; 
     RegistrationDescription registration = null; 

     // Check for existing registrations. 
     foreach (var registrationDescription in regsForInstId) 
     { 
      if (firstRegistration) 
      { 
       // Update the tags. 
       registrationDescription.Tags = new HashSet<string>() { installationId, userName }; 

       // We need to handle each platform separately. 
       switch (platform) 
       { 
        case "android": 
         var gcmReg = registrationDescription as GcmRegistrationDescription; 
         gcmReg.GcmRegistrationId = registrationId; 
         registration = await _hubClient.UpdateRegistrationAsync(gcmReg); 
         break; 
       } 
       updated = true; 
       firstRegistration = false; 
      } 
      else 
      { 
       // We shouldn't have any extra registrations; delete if we do. 
       await _hubClient.DeleteRegistrationAsync(registrationDescription); 
      } 
     } 

     // Create a new registration. 
     if (!updated) 
     { 
      switch (platform) 
      { 
       case "android": 
        registration = await _hubClient.CreateGcmNativeRegistrationAsync(registrationId, new[] { installationId, userName }); 
        break; 
      } 
     } 

     return registration; 
    } 

    // Basic implementation that sends a notification to Android clients 
    public async Task<bool> SendNotification(int id, string from, string text, string tag) 
    { 
     try 
     { 
      var payload = new 
      { 
       data = new 
       { 
        message = new 
        { 
         // these properties can be whatever you want 
         id, 
         from, 
         text, 
         when = DateTime.UtcNow.ToString("s") + "Z" 
        } 
       } 
      }; 

      var json = JsonConvert.SerializeObject(payload); 

      await _hubClient.SendGcmNativeNotificationAsync(json, tag); 

      return true; 
     } 
     catch (ArgumentException ex) 
     { 
      // This is expected when an APNS registration doesn't exist. 
      return false; 
     } 
    } 

    public async Task<bool> ClearRegistrations(string userName) 
    { 
     // Get registrations for the current installation ID. 
     var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(userName, 100); 

     // Check for existing registrations. 
     foreach (var registrationDescription in regsForInstId) 
     { 
      // We shouldn't have any extra registrations; delete if we do. 
      await _hubClient.DeleteRegistrationAsync(registrationDescription); 
     } 
     return true; 
    } 
} 

您的Android客戶端需要在啓動過程中調用您的後端的註冊API。我有一個MVC的行動。

[HttpPost] 
public async Task<ActionResult> Register(string platform, string installationId, string registrationId, string userName) 
{ 
    try 
    { 
     var hub = new AndroidNotificationHub(); 
     var registration = await hub.Register(platform, installationId, registrationId, userName); 
     return Json(registration); 
    } 
    catch (Exception ex) 
    { 
     return Content(ex.ToString()); 
    } 
} 

一旦移動客戶端已註冊,你就可以開始從後臺發送通知,通過調用SendNotification方法。

希望這點能指引您朝着正確的方向發展。

+0

你好@Kiliman,我們發送給Register函數的'installationId'值是什麼樣的信息? – andrefadila

+0

對於其他任何人而言,這看起來像installationId只是一個應用程序特定的值,只是作爲標籤使用,因此對註冊沒有任何關鍵。 – Jason

相關問題