Xamarin IOS 上的推送通知
本文关键字:通知 IOS Xamarin | 更新日期: 2023-09-27 18:31:36
我们正在使用Xamarin.Forms在Xamarin上开发一个跨平台(iOS和Android)应用程序。我们已经设法让相同的应用程序在IOS和Android上运行。伟大!
我们希望在我们的应用程序中包含推送通知,这已经在 Android 中运行。然而,对于IOS来说,这是一个完全不同的故事。找不到裸露的图书馆,任何地方!!
对于Android,我们使用Redth的Google Cloud Messaging Client,这个库非常易于使用。我们在 2 小时或更短的时间内运行了它。但是,对于IOS来说,找不到这样的东西。
如何在 xamarin 中注册我的 IOS 设备以推送通知?我们已经有了正确的证书等,它只是我们需要开始工作的设备端。有什么东西可以指出我正确的方向吗?
使用此推送通知链接在 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);
}
}
}
}