我们正在使用Xamarin.Forms在Xamarin上开发一个跨平台(ios和android)应用程序。我们设法让相同的应用程序在IOS和Android上运行。大!
我们希望在我们的应用中包含推送通知,这已经适用于Android。然而,对于IOS来说,这是一个完全不同的故事..没有找到裸骨库,无处不在!
对于Android,我们使用了Redth的Google Cloud Messaging Client,这个库非常易于使用。我们让它在2小时或更短的时间内运行。但是,IOS没有找到这样的东西。
如何在xamarin中注册我的IOS设备以进行推送通知?我们已经拥有了正确的证书等,它只是我们需要工作的设备方面。有什么能指引我正确的方向吗?
使用此push notification链接在IOS中获取或启用推送通知服务。
您需要执行以下步骤:
1>创建和下载SSL和APNS证书以及推送通知启用的配置文件。
2>首先双击SSL证书而不是APNS证书而不是配置文件。
3.>现在从密钥链访问中导出p12文件,并从命令提示符创建PEM文件。
4>现在在FinishedLaunching
内注册推送通知。
5.>运行程序,您将获得设备令牌并将其发送到服务器。
现在,APNS服务器会将此通知分别发送给令牌。
您可能需要查看PushSharp以支持iOS和Android通知的服务器端部分。我相信他们也有一个Xamarin.iOS库,您可以使用它来订阅推送通知。
// Register for push notifications.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var authOptions = UserNotifications.UNAuthorizationOptions.Alert | UserNotifications.UNAuthorizationOptions.Badge | UserNotifications.UNAuthorizationOptions.Sound;
UserNotifications.UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
Console.WriteLine(granted);
});
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
}
private SBNotificationHub Hub { get; set; }
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);
//if user is not logged In
Employee employee = JsonConvert.DeserializeObject<Employee>(Settings.CurrentUser);
if (employee != null)
{
NSSet tags = new NSSet(new string[] { "username:" + employee.Email }); // create tags if you want
Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
{
if (errorCallback != null)
Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
});
}
else
{
Hub.UnregisterAllAsync(deviceToken, (error) =>
{
if (error != null)
{
Console.WriteLine("Error calling Unregister: {0}", error.ToString());
return;
}
});
}
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
AzurePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
ProcessNotification(userInfo, false);
}
void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
{
// Check to see if the dictionary has the aps key. This is the notification payload you would have sent
if (null != options && options.ContainsKey(new NSString("aps")))
{
//Get the aps dictionary
NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string alert = string.Empty;
if (aps.ContainsKey(new NSString("alert")))
alert = (aps[new NSString("alert")] as NSString).ToString();
if (!fromFinishedLaunching)
{
//Manually show an alert
if (!string.IsNullOrEmpty(alert))
{
NSString alertKey = new NSString("alert");
UILocalNotification notification = new UILocalNotification();
notification.FireDate = NSDate.Now;
notification.AlertBody = aps.ObjectForKey(alertKey) as NSString;
notification.TimeZone = NSTimeZone.DefaultTimeZone;
notification.SoundName = UILocalNotification.DefaultSoundName;
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
}
}
}