Following this tutorial我创建了 azure 通知中心并添加到我的应用程序注册中。我的应用程序注册远程通知成功,当我尝试在后端注册设备时,我成功获得了我的安装 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 发送通知时 - 它会发送。 (虽然我没有收到,但这已经是另一个问题了)。
更新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);
顺便说一句,notificationId 为空,状态通知显示为“已入队”。
最佳答案
我最终从头开始学习了教程,重新创建了证书、配置文件、通知中心等,现在它可以工作了。
关于ios - Azure 通知中心不适用于 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36956993/