6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iOS13のPush通知に必要なDeviceToken取得に関して

Last updated at Posted at 2019-12-24

はじめに

最近起こったこと。。
サーバーからPUSH通知を送りたいために
クライアント側でDeviceTokenを取得し、サーバー側に送って保存しているが
うまく保存できずエラーが出ていた

原因

XCodeをVersion11にあげたことにより
iOS13以降の端末でDeviceTokenの形式が変わってしまったようです

原因となったコード

@ Objective-c

+ (void)getDeviceToken:(NSData * )deviceToken
{
       NSString *deviceTokenSt = [[[[deviceToken description]
                                 stringByReplacingOccurrencesOfString:@"<" withString:@""]
                                stringByReplacingOccurrencesOfString:@">" withString:@""]
                               stringByReplacingOccurrencesOfString: @" " withString: @""];
}

▼このような形式になってしまいます。一部文字列が...に置き換わってしまいエラーになる
{length=32,bytes=0xcc**********956d55**********3f46...e0950**********8}

対応方法

+ (void)getDeviceToken:(NSData * )deviceToken
{
    NSUInteger length = deviceToken.length;
    if (length == 0) {
        return;
    }
    const unsigned char *buffer = (unsigned char*)deviceToken.bytes;
    NSMutableString *hexString  = [NSMutableString stringWithCapacity:(length * 2)];
    for (int i = 0; i < length; ++i) {
        [hexString appendFormat:@"%02x", buffer[i]];
    }
    NSString *deviceTokenSt = [hexString copy];
    [NpSendMessengerObject_objc sendMessage:@"_pushReceiver" method:@"receiver" sendString:deviceTokenSt];
}

▼OK!
cc**********956d55**********3f462fdd**********3ce0950**********8

環境

iOS 13.3
XCode Version11.3

参考

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?