私はMac用のMonotouchを使用しており、プロセスでプッシュ通知を有効にするプロビジョニングプロファイル証明書を取得する手順を実行しました。私は動作するアプリを持っていて、現在apns-sharpとmoon-apnsを試していますが、デバイストークンを取得する方法を理解できません。誰かがこれを達成するための詳細で簡単な手順を私に提供してくれることを願っています。
3 に答える
メソッドで、取得したオブジェクトを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
}
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);
}
私にとって、これは解像度の半分にすぎませんでした。Webサーバー(私の場合はPHP)からDeviceTokenを使用するには、DeviceTokenがプッシュ通知を起動するためのPHPコードで使用される16進文字列である必要があります(例:[ PHPを使用してAPNを介してiOSプッシュ通知を送信する
ただし、NSdataオブジェクトは、その16進文字列を提供する簡単な方法を提供しません。
したがって、私の「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");
}
}
また、バイトから16進数への変換の場合:
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送信を作成できます。