Following this tutorial我创建了azure通知中心并添加到我的应用注册中。我的app注册了远程通知成功,当我试图在后端获得注册设备时,我成功获得了我的安装ID。
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
MobileServiceClient client = QSTodoService.DefaultService.GetClient;
const string templateBodyAPNS = "{\"aps\":{\"alert\":\"$(messageParam)\"}}";
JObject templates = new JObject();
templates["genericMessage"] = new JObject
{
{"body", templateBodyAPNS}
};
// Register for push with your mobile app
var push = client.GetPush();
push.RegisterAsync(deviceToken, templates);
}
但是在尝试发送通知时
// Get the settings for the server project.
HttpConfiguration config = this.Configuration;
MobileAppSettingsDictionary settings =
this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
// Get the Notification Hubs credentials for the Mobile App.
string notificationHubName = settings.NotificationHubName;
string notificationHubConnection = settings
.Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
// Create a new Notification Hub client.
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
// Sending the message so that all template registrations that contain "messageParam"
// will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations.
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["messageParam"] = "some message";
try
{
var result = hub.SendTemplateNotificationAsync(templateParams);
}
catch (System.Exception ex)
{
config.Services.GetTraceWriter()
.Error(ex.Message, null, "Push.SendAsync Error");
}
结果说“成功发送:0,发送失败:0。
造成这种麻烦的原因是什么?
更新
当我尝试通过azure发送通知时,我不认为sertificate或配置文件的问题 - 它发送。 (虽然我没有收到,但这已经是另一个问题)。
更新2
我也尝试发送到注册标签,
string payload = "{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";
List<string> tags = new List<string> {"testTag"};
hub.CreateAppleTemplateRegistrationAsync(token,payload,tags);
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["messageParam"] = "message";
var res = hub.SendTemplateNotificationAsync(templateParams, tags);
和苹果原生通知,但结果是一样的。
hub.SendAppleNativeNotificationAsync(payload);
BTW,notificationId为空,状态通知显示为“已排队”。
答案 0 :(得分:0)
我最终从头开始学习教程,重新创建证书,配置文件,通知中心等,现在它可以工作。