来自 Azure 通知中心的推送通知 (Xamarin.iOS)
Push notifications from Azure Notification Hub (Xamarin.iOS)
我已经尝试了 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 测试发送我得到 "Message was successfully sent, but there were no matching targets."
我按原样执行了所有步骤,但是当我设置断点时,我总是得到 "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 推送通知?
注意:我正在使用
- Visual Studio 2017(版本 15.4.0)于 Windows 10
- Mac OS High Siera 版本 10.13
- Xcode 版本 9.0.1
- VS Mac 社区版本 7.2(内部版本 636)
- 测试 iPhone 5S,iPhone 6,iPhone 6S,iPhone 7,iPhone 8 - iOS11.0
使用物理 iPhone 设备解决。
"The simulator does not do Push Notifications. And to push from a server, you have to have device(s) to push to as well as your app on that device."
"The iPhone Simulator is unable to receive push notifications or successfully register for them."
我已经尝试了 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 测试发送我得到 "Message was successfully sent, but there were no matching targets."
我按原样执行了所有步骤,但是当我设置断点时,我总是得到 "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 推送通知?
注意:我正在使用
- Visual Studio 2017(版本 15.4.0)于 Windows 10
- Mac OS High Siera 版本 10.13
- Xcode 版本 9.0.1
- VS Mac 社区版本 7.2(内部版本 636)
- 测试 iPhone 5S,iPhone 6,iPhone 6S,iPhone 7,iPhone 8 - iOS11.0
使用物理 iPhone 设备解决。
"The simulator does not do Push Notifications. And to push from a server, you have to have device(s) to push to as well as your app on that device."
"The iPhone Simulator is unable to receive push notifications or successfully register for them."