tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.2k stars 1.46k forks source link

Using registerDevice with APNS: No notifications showing #873

Closed kiasaki closed 4 years ago

kiasaki commented 4 years ago

Hello there TDLib devs!

I have a mobile iOS app that is using TDLib and I want to get background push notifications working, I initially tried using FCM/GCM/Firebase because I used that in the Android version of my app successfully but I haven't been able to get "background"/"remote" notifications with it.

So I setup a separate telegram account & app id to use Apple/APNS, uploaded certificates and am calling registerDevice but no notifications seem to come through on the device (I am aware that the simulator doesn't support push/background/remote notifications and I have to use a real device to test).

Any clues on what I might have missed? I looked at the ObjC & Swift versions of the official clients and they don't seem to be using registerDevice different than I am.

Here are the logs with the verbosity level set to 4 from startup up to the successful call to registerDevice:

[ 3][t 4][1578507281.403331280][Td.cpp:3548][#1][!Td][&td_requests]  Receive request 2: setTdlibParameters {
  parameters = tdlibParameters {
    use_test_dc = false
    database_directory = "/var/mobile/Containers/Data/Application/57B2E988-6076-48AB-9210-D8271D0D2CE1/Documents/td0"
    files_directory = "/var/mobile/Containers/Data/Application/57B2E988-6076-48AB-9210-D8271D0D2CE1/Documents/td0"
    use_file_database = true
    use_chat_info_database = true
    use_message_database = true
    use_secret_chats = true
    api_id = 1121861
    api_hash = "6cd2261d5431REDACTED"
    system_language_code = "en-US"
    device_model = "Telefuel App"
    system_version = "Mobile"
    application_version = "0.0.1"
    enable_storage_optimizer = true
    ignore_file_names = false
  }
}
[ 3][t 0][1578507281.403609276][Client.cpp:276][&td_requests]    End to wait for updates, returning object 1 0x28237b480
2020-01-08 13:14:41.405253-0500 Runner[2268:133936] flutter: tdstate: receive: {@type: ok}
[ 3][t 4][1578507281.406280994][Binlog.cpp:371][#1][!Td] Load: init encryption
[ 3][t 4][1578507281.406496048][Td.cpp:4657][#1][!Td][&td_requests]  Sending update: updateAuthorizationState {
  authorization_state = authorizationStateWaitEncryptionKey {
    is_encrypted = true
  }
}
[ 3][t 4][1578507281.406516075][Td.cpp:4672][#1][!Td][&td_requests]  Sending result for request 2: ok {
}
[ 3][t 0][1578507281.406789303][Client.cpp:270][&td_requests]    Begin to wait for updates with timeout 1.000000
[ 3][t 0][1578507281.406924248][Client.cpp:276][&td_requests]    End to wait for updates, returning object 0 0x282361120
2020-01-08 13:14:41.408187-0500 Runner[2268:133936] flutter: tdstate: receive: {@type: updateAuthorizationState, authorization_state: {@type: authorizationStateWaitEncryptionKey, is_encrypted: true}}
[ 3][t 0][1578507281.408890247][Client.cpp:270][&td_requests]    Begin to wait for updates with timeout 1.000000
[ 3][t 4][1578507281.408884048][Td.cpp:3548][#1][!Td][&td_requests]  Receive request 3: checkDatabaseEncryptionKey {
  encryption_key = bytes [18] { B5 REDACTED }
}
[ 3][t 0][1578507281.409105062][Client.cpp:276][&td_requests]    End to wait for updates, returning object 2 0x282360cb0
[ 3][t 4][1578507281.409633160][Binlog.cpp:371][#1][!Td] Load: init encryption
2020-01-08 13:14:41.410163-0500 Runner[2268:133936] flutter: tdstate: receive: {@type: ok}
[ 3][t 0][1578507281.410676003][Client.cpp:270][&td_requests]    Begin to wait for updates with timeout 1.000000
[ 2][t 4][1578507281.438338041][TdDb.cpp:313][#1][!Td]   Got PRAGMA user_version = 9
[ 3][t 4][1578507281.438400030][DialogDb.cpp:27][#1][!Td]    Init dialog database [version:9]
[ 3][t 4][1578507281.438746214][MessagesDb.cpp:44][#1][!Td]  Init message database [version:9]
[ 3][t 4][1578507281.438841105][FileDb.cpp:44][#1][!Td]  Init file database [version:9]
[ 3][t 4][1578507281.439721107][Td.cpp:4269][#1][!Td]    Successfully inited database in [database_directory:/private/var/mobile/Containers/Data/Application/57B2E988-6076-48AB-9210-D8271D0D2CE1/Documents/td0/] and [files_directory:/private/var/mobile/Containers/Data/Application/57B2E988-6076-48AB-9210-D8271D0D2CE1/Documents/td0/]
[ 4][t 4][1578507281.439844131][Global.cpp:82][#1][!Td] LOAD: [server_time_difference:1578481884.288023]
[ 3][t 4][1578507281.440418243][NetQueryDispatcher.cpp:281][#1][!Td] [main_dc_id:1]
[ 4][t 4][1578507281.442388058][UpdatesManager.cpp:912][#1][!Td]    Init: 29340 0 1578498767
[ 3][t 4][1578507281.442439079][SecretChatsManager.cpp:124][#1][!SecretChatsManager] Init secret chats qts [qts:0]
[ 3][t 4][1578507281.442449093][UpdatesManager.cpp:211][#1][!Td][&get_difference]    -----BEGIN GET DIFFERENCE----- from init_state
[ 3][t 4][1578507281.442799091][UpdatesManager.cpp:135][#1][!Td][&get_difference]    [pts:29340][qts:0][date:1578498767]
[ 3][t 3][1578507281.443182230][ResourceManager.cpp:136][#1][!UploadResourceManager] [unused:2097152]
[ 4][t 4][1578507281.442869186][utils.cpp:21][#1][!Td]  Create storer for updates_getDifference {
  flags = 0
  pts = 29340
  date = 1578498767
  qts = 0
}
[ 3][t 4][1578507281.443267107][NetQuery.h:347][#1][!Td] [Query:[id:65536][tl:0x25939651][state:Query]]
[ 3][t 4][1578507281.443279982][Td.cpp:3693][#1][!Td][&net_query]    Send [Query:[id:65536][tl:0x25939651][state:Query]] to dispatcher
[ 3][t 4][1578507281.443288088][NetQuery.h:239][#1][!Td][&net_query] [Query:[id:65536][tl:0x25939651][state:Query]] [debug:Td: send to NetQueryDispatcher]
[ 3][t 4][1578507281.443301201][NetQuery.h:239][#1][!Td][&net_query] [Query:[id:65536][tl:0x25939651][state:Query]] [debug:dispatch]
[ 2][t 4][1578507281.443322182][AuthDataShared.cpp:112][#1][!Td] DcId{1} [auth_key_id:3152421900467331432][state:OK]
[ 3][t 4][1578507281.443356037][NetQuery.h:239][#1][!Td][&net_query] [Query:[id:65536][tl:0x25939651][state:Query]] [debug:sent to main session multi proxy DcId{1}]
[ 4][t 4][1578507281.443392038][Timeout.cpp:19][#1][!Td]    Set AlarmTimeout for -2 in -0.000005
[ 4][t 4][1578507281.443403006][Timeout.cpp:76][#1][!Td]    Set timeout of AlarmTimeout in -0.000014
[ 3][t 4][1578507281.444452286][ConnectionCreator.cpp:1017][#1][!ConnectionCreator]  SAVE DcOptions{[DcOption:DcId{1}[ip:149.154.175.58][port:443][secret_len:0][flags:]], [DcOption:DcId{1}[ip:149.154.175.55][port:443][secret_len:0][flags:(Static)]], [DcOption:DcId{1}[ip:2001:b28:f23d:f001::a][port:443][secret_len:0][flags:(IPv6)]], [DcOption:DcId{2}[ip:149.154.167.51][port:443][secret_len:0][flags:]], [DcOption:DcId{2}[ip:149.154.167.51][port:443][secret_len:0][flags:(Static)]], [DcOption:DcId{2}[ip:149.154.167.151][port:443][secret_len:0][flags:(MediaOnly)]], [DcOption:DcId{2}[ip:2001:67c:4e8:f002::a][port:443][secret_len:0][flags:(IPv6)]], [DcOption:DcId{2}[ip:2001:67c:4e8:f002::b][port:443][secret_len:0][flags:(MediaOnly)(IPv6)]], [DcOption:DcId{3}[ip:149.154.175.100][port:443][secret_len:0][flags:]], [DcOption:DcId{3}[ip:149.154.175.100][port:443][secret_len:0][flags:(Static)]], [DcOption:DcId{3}[ip:2001:b28:f23d:f003::a][port:443][secret_len:0][flags:(IPv6)]], [DcOption:DcId{4}[ip:149.154.167.91][port:443][secret_len:0][flags:]], [DcOption:DcId{4}[ip:149.154.167.91][port:443][secret_len:0][flags:(Static)]], [DcOption:DcId{4}[ip:2001:67c:4e8:f004::a][port:443][secret_len:0][flags:(IPv6)]], [DcOption:DcId{4}[ip:149.154.165.120][port:443][secret_len:0][flags:(MediaOnly)]], [DcOption:DcId{4}[ip:2001:67c:4e8:f004::b][port:443][secret_len:0][flags:(MediaOnly)(IPv6)]], [DcOption:DcId{5}[ip:2001:b28:f23f:f005::a][port:443][secret_len:0][flags:(IPv6)]], [DcOption:DcId{5}[ip:91.108.56.100][port:443][secret_len:0][flags:(Static)]], [DcOption:DcId{5}[ip:91.108.56.100][port:443][secret_len:0][flags:]]}
[ 3][t 4][1578507281.444787025][ConfigManager.cpp:677][#1][!Recoverer][&config_recoverer]    Successfully connected
[ 3][t 4][1578507281.444878101][ConfigManager.cpp:740][#1][!Recoverer][&config_recoverer]    Wakeup NEVER
[ 4][t 4][1578507281.444911242][utils.cpp:21][#1][!ConfigManager]   Create storer for help_getConfig {
}
[ 3][t 4][1578507281.444998026][NetQuery.h:347][#1][!ConfigManager]  [Query:[id:131072][tl:0xc4f9186b][state:Query]]
[ 3][t 4][1578507281.445008993][NetQuery.h:239][#1][!ConfigManager][&net_query]  [Query:[id:131072][tl:0xc4f9186b][state:Query]] [debug:dispatch]
[ 3][t 4][1578507281.445019007][NetQuery.h:239][#1][!ConfigManager][&net_query]  [Query:[id:131072][tl:0xc4f9186b][state:Query]] [debug:sent to main session multi proxy DcId{1}]
[ 3][t 4][1578507281.445220232][DeviceTokenManager.cpp:320][#1][!DeviceTokenManager] GET device token 1--->Synchronized token "915D65331E964EDF21CC467REDACTED"
[ 4][t 4][1578507281.445256948][utils.cpp:21][#1][!DeviceTokenManager]  Create storer for account_registerDevice {
  token_type = 1
  token = "915D65331E964EDF21CC467REDACTED"
  app_sandbox = false
  secret = bytes [0] { }
  other_uids = vector[0] {
  }
}
[ 3][t 4][1578507281.445379257][NetQuery.h:347][#1][!DeviceTokenManager] [Query:[id:196608][tl:0x5cbea590][state:Query]]
[ 3][t 4][1578507281.445389271][NetQuery.h:239][#1][!DeviceTokenManager][&net_query] [Query:[id:196608][tl:0x5cbea590][state:Query]] [debug:dispatch]
[ 3][t 4][1578507281.445397139][NetQuery.h:239][#1][!DeviceTokenManager][&net_query] [Query:[id:196608][tl:0x5cbea590][state:Query]] [debug:sent to main session multi proxy DcId{1}]
[ 4][t 4][1578507281.445552111][TopDialogManager.cpp:501][#1][!TopDialogManager]    Init is enabled: true
[ 3][t 4][1578507281.445680141][TopDialogManager.cpp:603][#1][!TopDialogManager] Wakeup in: 75443.554317
[ 3][t 4][1578507281.445962191][StorageManager.cpp:325][#1][!StorageManager] Schedule next file gc in 534
[ 3][t 4][1578507281.445995092][StorageManager.cpp:231][#1][!StorageManager] Loaded fast storage statistics with 12 files of total size 68653
levlam commented 4 years ago

What is your user_id?

kiasaki commented 4 years ago

User ID that owns this app (App ID 1121861) is 928699539 (@estebanoc). My personal User ID is 532457147 (@kiasaki).

levlam commented 4 years ago

@kiasaki Could you try again (it is expected to fail) and tell exact time when the push notification was expected to be received?

kiasaki commented 4 years ago

Sure thing, I should have received a few just now. At around 4:55 EST on January 10, 2020.

kiasaki commented 4 years ago

@levlam Where you able to find some logs about this? Can you point me to some code that actually uses APNS to receive notifications?

I've tried using Firebase Messaging on iOS without success too.

levlam commented 4 years ago

@kiasaki We haven't found anything suspicious regarding your app. The uploaded APNS certificate looks correct.

TDLib just registers device server-side when the method registerDevice is called: https://github.com/tdlib/td/blob/0dcbe4973afe4770785b7286994745e5704fbd34/td/telegram/DeviceTokenManager.cpp#L380

After that push notifications are supposed to be sent by the server to APNS, then forwarded to the device, catched by the application and passed to the TDLib through processPushNotification method if possible.

levlam commented 4 years ago

@kiasaki Server fails to connect to APNS push notification server ssl://gateway.push.apple.com:2195 with the error "Failed to use private key". So, it seems that something is wrong with the key.

kiasaki commented 4 years ago

@levlam Oh! Thanks for the information. That's great news! I'll look into those then. Thanks for the help debugging this.