iOSのバージョンを13に上げた途端に、Push通知に使われているトークン文字列が変な値になっていて、通知が届かないという事象が発生したのでその対応をメモ。
環境
Mac Mojave 10.14.6
xcode 11(おそらく10以下でも発生する)
iOS 13.4.1
Objective-C
修正
届かなくなってしまった方は次のようなコードがあると思うのでそちらを修正します。
修正前
HogeHugaAppDelegate.m
| 
					 1 2 3 4 5 6  | 
						- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {     //デバイストークン取得後の処理 "<" ">" " "を削除     NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]                    stringByReplacingOccurrencesOfString:@">" withString:@""]                    stringByReplacingOccurrencesOfString: @" " withString: @""]; }  | 
					
修正後
HogeHugaAppDelegate.m
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 
						- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {     NSString *token = [self hexadecimalStringFromData:deviceToken]; } - (NSString *)hexadecimalStringFromData:(NSData *)data {     NSUInteger dataLength = data.length;     if (dataLength == 0) {         return nil;     }     const unsigned char *dataBuffer = data.bytes;     NSMutableString *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];     for (int i = 0; i < dataLength; ++i) {         [hexString appendFormat:@"%02x", dataBuffer[i]];     }     return [hexString copy]; }  | 
					
(参考)FaceBook Git
動作しなくなった原因
iOSのバージョンアップによって、descriptionの返り値が変更されたことが原因のよう。
https://forums.developer.apple.com/thread/117545
| OS | descriptionの返り値 | 
|---|---|
| iOS12以前 | <00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000> | 
| iOS13 | {length = 32, bytes = 0x00000000 00000000 00000000 00000000 … 00000000 00000000 } | 
いや、そんなとこ変えんなや笑
なお、Swiftを使っている場合には最初からこの記述方法になっているとのことなので修正の必要はなさそう。
もしSwiftなのに動かないよって方がいらっしゃったらこちらが参考になるかもです。
最後に -情報のキャッチについて-
今回の修正が必要ですよというの、実はApple Developperのフォーラムではにはだいぶ前からわかっていたらしいです。
なのでちゃんと情報をチェックしていた人はあたふたすることなくiOSのバージョンアップに対応出来たのでしょう。。。
iOSのエンジニアをやっている方は定期的にチェックをするようにしておいたほうが良さそうですね。
iOS13 PKPushCredentials broken | Apple Developer Forums
それではみなさん良い開発ライフを!!
  
  
  
  



コメント