2014-09-25 19 views
1

我想弄清楚處理這種情況的最佳方法。ASP.NET MVC Azure應用程序iOS推送通知

我在Azure中有一個MVC後端,它爲我的應用程序提供了一堆端點。我想添加另一個端點來處理向用戶發送iOS推送通知。

我正在處理存儲他們的設備令牌,所以我雖然只是使用PushSharp並遍歷每個令牌並將相應的令牌推送給用戶。這在當地很有用,但我似乎無法讓PushSharp在Azure中工作。我已經看到了這個解決方案在這裏:

https://github.com/Redth/PushSharp/wiki/Configuring-a-certificate-for-Apple-Push-Notifications-Service-on-the-Azure-platform

但是我不確定什麼ServiceDefinition.csdef文件,以及如何將納入我的MVC解決方案。

我已經開始研究Azure通知中心,但我不確定它可以做我需要的。我只是在C#中尋找一個簡單的解決方案,該解決方案可以託管在Azure(ASP.NET MVC)中,我可以遍歷一組設備令牌並推送通知。

回答

4

如果您已經有Azure託管的MVC應用程序從終端用戶iOS應用程序獲取並存儲設備令牌,那麼您可以在其中創建Service Bus名稱空間和通知中心,上傳APNS證書,然後您的MVC應用程序就能夠註冊令牌並使用Microsoft.ServiceBus.dll發送通知。

這裏是非常基本的代碼示例:

var notificationHubClint = NotificationHubClient.CreateClientFromConnectionString("{connection string from the portal}", "{your hub name}"); 

// Register device token with notification hub. Call it when you get device token from iOS app first time. 
var registration = await notificationHubClint.CreateAppleNativeRegistrationAsync("{device token}",new []{"SomeUserId"}); 

// You can modify properties or refresh device token and then update registration 
registration.DeviceToken = "{new token}"; 
registration.Tags.Add("{new tag}"); 
await notificationHubClint.UpdateRegistrationAsync(registration); 

// Send notification. Call when you want to send notification to user. 
await notificationHubClint.SendAppleNativeNotificationAsync("{your message}","some user id"); 

在這裏,我使用的標籤定位到該消息的特定用戶。如果您僅調用SendAppleNativeNotificationAsync(「{your message}」),則消息將傳遞給所有用戶。瞭解more about tags and tag expressions以使您的發送效率更高。

+0

嗨,謝謝你有這樣的代碼嗎?我似乎無法弄清楚如何使用設備令牌與ServiceBus DLL – aherrick 2014-09-25 17:48:07

+1

我已更新帖子。 – efimovandr 2014-09-25 20:20:07

+0

這看起來正是我需要的,謝謝! – aherrick 2014-09-25 20:22:17