【发布时间】:2018-04-28 07:49:37
【问题描述】:
我已经尝试过 Android 和 Windows 推送通知,一切正常。但是,在 iOS 端,有些问题。
我正在尝试使用这些教程向我的 Xamarin.iOS 应用添加推送通知
- https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started
- https://blog.xamarin.com/push-notifications-ios-azure-notification-hubs/
从 Azure 测试发送我得到“消息已成功发送,但没有匹配的目标。”
我已经按照原样执行了所有步骤,但是当我设置断点时,我总是得到“IsRegistered =false”:
AppDelegate.cs
private SBNotificationHub Hub { get; set; }
public const string ConnectionString = "Endpoint=sb://...";
public const string NotificationHubPath = "NotificationHubName...";
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Set our preferred notification settings for the app
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
// Register for notifications
UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
bool IsRegistered = UIApplication.SharedApplication.IsRegisteredForRemoteNotifications;
return true;
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Hub = new SBNotificationHub(ConnectionString, NotificationHubPath);
Hub.UnregisterAllAsync(deviceToken, (error) =>
{
if (error != null)
{
// Error unregistering
return;
}
// Register this device with the notification hub
Hub.RegisterNativeAsync(deviceToken, null, (registerError) =>
{
if (registerError != null)
{
// Error registering
}
});
});
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
if (null != userInfo && userInfo.ContainsKey(new NSString("aps")))
{
// Get the aps dictionary from the alert payload
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
}
}
我的问题是;
- 为什么我没有注册?
- 我怎样才能知道我为什么不注册?
- Xamarin 是否支持来自 Azure 通知中心的 iOS 11.0 推送通知?
注意:我正在使用
- Windows 10 上的 Visual Studio 2017(版本 15.4.0)
- Mac OS High Siera 版本 10.13
- Xcode 版本 9.0.1
- VS for Mac Community 版本 7.2(内部版本 636)
- 在 iPhone 5S、iPhone 6、iPhone 6S、iPhone 7、iPhone 8 - iOS11.0 上测试
【问题讨论】:
-
显示
RegisteredForRemoteNotifications方法中的代码。你在那里的通知中心注册了吗? -
我已在问题中添加了 RegisteredForRemoteNotifications 方法。
-
你有没有使用断点查看结果是什么?很有可能永远不会向 Azure 进行注册。但是没有对错误做任何事情,所以你不会知道
-
是的,我使用断点查看结果。 IsRegistered 始终为假。我再次检查了 Azure 中的 ConnectionString 和 NotificationHubName,并且与我项目中的字段相同。
标签: xamarin push-notification xamarin.ios xamarin.forms apple-push-notifications