我正在使用Monotouch for mac,并且已经完成了检索供应配置文件证书的步骤,在此过程中启用了推送通知。我有一个实用的应用程序,现在正在试验app -夏普和月亮--app,但是我想不出如何来检索我的设备令牌。我希望有人能为我提供的详细和简单的步骤来实现这一点。
发布于 2012-03-13 09:58:56
在您的FinishedLaunching方法中,通过UIApplication对象注册应用程序以进行远程通知:
// Pass the UIRemoteNotificationType combinations you want
app.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Alert |
UIRemoteNotificationType.Sound);然后,在AppDelegate类中,重写RegisteredForRemoteNotifications方法:
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
// The device token
byte[] token = deviceToken.ToArray();
}您还必须重写FailedToRegisterForRemoteNotifications方法,以处理错误(如果有的话):
public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
// Do something with the error
}发布于 2019-11-26 21:44:14
到了iOS,deviceToken已经改变了。下面的代码为我将deviceToken转换为NSData转换为字符串起了作用。
string deviceTokenString;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
deviceTokenString = BitConverter.ToString(deviceToken.ToArray()).Replace("-", string.Empty);
}
else
{
deviceTokenString = Regex.Replace(deviceToken.ToString(), "[^0-9a-zA-Z]+", string.Empty);
}发布于 2019-10-22 18:15:14
对我来说,这只是决议的一半。要使用来自webserver的DeviceToken (在我的例子中是DeviceToken ),DeviceToken需要是PHP代码中用于触发Push通知的十六进制字符串(例如:(使用DeviceToken通过DeviceToken发送iOS推送通知)
但是,NSdata对象不提供提供十六进制字符串的简单方法。
所以我的"RegisteredForRemoteNotifications“成功处理程序现在是:
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
// Get current device token
var DeviceToken = Tools.ByteToHex(deviceToken.ToArray());
string DeviceID = UIDevice.CurrentDevice.IdentifierForVendor.AsString();
System.Console.WriteLine("### UserNotification Device Token = " + DeviceToken + ", DeviceID = " + DeviceID);
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
// Has the token changed?
if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
//### todo: Populate POSTdata set
//### todo: Send POSTdata to URL
// Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}
}和拜德到十六进制的转换:
public static string ByteToHex(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 2);
foreach (byte b in data)
{
sb.AppendFormat("{0:x2}", b);
}
return sb.ToString();
}现在,您可以在PHP中使用DeviceToken创建PushNotification提交。
https://stackoverflow.com/questions/9681035
复制相似问题