##リモートプッシュ通知用にdeviceTokenを取得したい
Xamarinで開発されたiOSアプリの改修をしています。
サーバサイドの担当者から、「iOS 10端末のデバイストークンが送られてこない」という報告があり調査したところ、送る以前に取得できていないようでした。
新仕様(UserNotificationFramework)への移行で解消するかと思いましたが、正常に動く状態にできていません。
書き方が悪いのか、そもそものアプローチが間違っているのか、お知恵を拝借できればと思います。
###元のコードがこんな感じです
C#
1if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 2{ 3 var pushSettings = UIUserNotificationSettings.GetSettingsForTypes( 4 UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, 5 new NSSet()); 6 7 UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings); 8 UIApplication.SharedApplication.RegisterForRemoteNotifications(); 9} 10else { 11 UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; 12 UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); 13}
Xcode8(Beta2)とiOS10ではremote push通知用のdeviceTokenが取得できず失敗する時のチェック
iOS10とiOS9にプッシュ通知を送るための最低限の処理
↑このあたりを参考にして手を加えてみました
###手を入れたコード
C#
1if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) 2{ 3 UNUserNotificationCenter.Current.RequestAuthorization( 4 UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, 5 (granted, error) => 6 { 7 if (granted) 8 { 9 UIApplication.SharedApplication.RegisterForRemoteNotifications(); 10 } 11 } 12 ); 13} 14else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) 15{ 16 var pushSettings = UIUserNotificationSettings.GetSettingsForTypes( 17 UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, 18 new NSSet()); 19 20 UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings); 21 UIApplication.SharedApplication.RegisterForRemoteNotifications(); 22} 23else { 24 UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; 25 UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); 26}
ver.10以上向けの処理部分のみ追加です
###出力されるエラーメッセージ
Warning: Unhandled exception: UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread. Unhandled managed exception: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread. (UIKit.UIKitThreadAccessException)
UIApplication.SharedApplication.RegisterForRemoteNotifications
以外の方法がないか調べてみたのですが、見つけることができませんでした。
####使用ツール
- XamarinStudio 6.1.5.0
- Xcode 8.2.1
####追記
色々試しまして、事態はまだ解決しておりませんが、問題がここではなさそうだったので一旦クローズとさせていただきます。
アドバイスくださったお二方ありがとうございました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/07 02:51 編集