tdlib / td

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

How do I use Firebase cloud messaging to receive push notifications #549

Closed mazenkh7 closed 5 years ago

mazenkh7 commented 5 years ago

In Android, Java; Since FCM, should be same as GCM I did the following after i created a Client object:

FirebaseInstanceId.getInstance().getInstanceId() .addOnCompleteListener(new OnCompleteListener() { @Override public void onComplete(@NonNull Task task) { if (!task.isSuccessful()) { return; } // Get new Instance ID token from Firebase token = task.getResult().getToken(); if (getToken() != null && !getToken().isEmpty()) client.send(new TdApi.RegisterDevice(new TdApi.DeviceTokenGoogleCloudMessaging(getToken()), null), null); } });

the token is registered successfully but i get no notifications at all, the function (onMessageReceived) doesn't get called.

is there anything else i should do? (i actually don't know what 'otherUserIDs' is and how to use it)

levlam commented 5 years ago

You have not passed a handler to the RegisterDevice request, so you don't really know whether request has succeded or returned an error.

First of all, you need to register your app at https://my.telegram.org, obtaining api_id and api_hash, choose app's operating system as Android and then upload FCM certificates for your app.

Between, in TDLib 1.4.0 DeviceTokenGoogleCloudMessaging was renamed to DeviceTokenFirebaseCloudMessaging and full push notification handling was implemented in TDLib. See https://core.telegram.org/tdlib/notification-api for more details.

mazenkh7 commented 5 years ago

Thanks a lot, I will update tdlib. But there still is a problem, I registered my app before in my.telegram.org There's no where to select the operating system as android also there's no where to upload said fcm certificates. And I can't delete my app nor create new one IMG_20190508_152617

levlam commented 5 years ago

@mazenkh7 You have already chosen right operating system. The only thing you need to do is to upload FCM API key in the "GCM API key" section.

mazenkh7 commented 5 years ago

Thanks a lot for your help, now after I did all that, I receive a "pushReceiver ID" from the registerDevice function, but that's all no notifications are received (onMessageReceived from the my FirebaseMessagingService is never called) or is this normal behavior and I should find the notifications elsewhere? I read this https://core.telegram.org/tdlib/notification-api but i didn't quite understand where the PUSH notifications are received. thanks.

(Also if there is a minimum working example for notifications, like the java example that would be great as I learned a lot from it)

levlam commented 5 years ago

To use TDLib Notification API you need to receive push notification as usually, then convert notification payload to JSON similar to examples in https://core.telegram.org/tdlib/notification-api#handling-push-notifications and pass it to ProcessPushNotification.

If you don't receive notifications through FirebaseMessagingService.onMessageReceived you need to recheck that server has accepted your GCM API key, that you use your API ID in the app and that you pass correct token to registerDevice. If this doesn't help please write API ID, user_id and other details to @tdlib_bot.

mazenkh7 commented 5 years ago

Thanks its showing successfully now, you're helpful and patient! The bot is amazing too.

I have another question though, why do i receive pending messages / delayed notifications? and what should I do if I know that I have delayed notifications.

levlam commented 5 years ago

If the user is offline, notifications are delayed in order to give him a chance to read the messages from a different device before the notification is shown.

MattNguyen commented 5 years ago

First of all, you need to register your app at https://my.telegram.org, obtaining api_id and api_hash, choose app's operating system as Android and then upload FCM certificates for your app.

@levlam I don't see this section in my.telegram.org anymore. Is it still possible to register FCM certs?

cc/ @kiasaki

Edit: We're developing on web.

MattNguyen commented 5 years ago

Looks like the push-notification option appears when you select android, not web. Is there a way to change the platform or revoke the api key/hash?

levlam commented 5 years ago

@MattNguyen FCM certificates is possible to register only if you are developing for Android and have chosen Android as operating system for your application. There is no way to change chosen platform after application is created.

ljmartinez1994 commented 4 years ago

Hi. I have already registered the device on Firebase, Telegram, have all. I run. Receive correctly the receiver id. So, my question is: Whats next? Because in the documentation say that have to enter on updateActiveNotifications and is not doing that. What i have to do,once i have the receiver ID?

levlam commented 4 years ago

ReceiverId is needed to launch correct TDLib instance with the same receiverId received from registerDevice. If you have only one account, then you don't need to use receiverId at all.

ljmartinez1994 commented 4 years ago

Accounts? Do you refer to user telegram account? I don undestand to you very well. Even is that, Tdlib is entering on some status, the user have pending notifications and tdlib do nothing.

levlam commented 4 years ago

@ljmartinez1994 Send logs with verbosity level 4 to @tdlib_bot.

bantonen commented 3 years ago

recheck that server has accepted your GCM API key

@levlam Hi, how can i check that the server has accepted my GCM API key?

levlam commented 3 years ago

@bantonen If you didn't receive an error while uploading the certificate at my.telegram.org, then it was accepted,

bantonen commented 3 years ago

@levlam but if I am using Android, as I understand it, I do not need a certificate, right?

levlam commented 3 years ago

@bantonen You need to upload a valid API key from console.cloud.google.com for your api ID at my.telegram.org. Otherwise, Telegram will not be able to send push notifications to your app.

bantonen commented 3 years ago

@levlam You mean these api keys https://firebase.google.com/docs/projects/api-keys#create-api-keys ?

levlam commented 3 years ago

@bantonen On this page there is a link to https://console.cloud.google.com/apis/credentials.

bantonen commented 3 years ago

@levlam I am loading this api key but no notifications are coming. Can you please tell me what the problem might be. My app id (492896)

levlam commented 3 years ago

@bantonen You also need to write your user_id to @tdlib_bot.

king-ma1993 commented 4 months ago

@levlam How can I know if the API key I set is correct? After setting it, I still don't receive notifications. I'm looking forward to your reply.

levlam commented 4 months ago

You will receive an error about invalid API keys for any request.

king-ma1993 commented 4 months ago

@levlam Where can I see the error messages? After setting the API key, I directly saved it and there was no error reported, but I'm developing an Android app.Can I add you on Telegram? I hope you can guide me through this. Thank you very much.

levlam commented 4 months ago

Ah, sorry. Currently, there is no way to troubleshoot missing or invalid FCM certificate, because errors happen much later than certificate is set. The app will receive no push notifications, and there is no way to find the reason.

king-ma1993 commented 4 months ago

@levlam Can you tell me where to obtain the correct API key? Currently, Google Firebase has been modified, and I don't know where to get the correct API key.

levlam commented 4 months ago

I don't know, where the certificate can be found now. Support for new FCM API will be added soon, but the old one still work and certificate for it should be available somewhere, unless Google intentionally removed possibility to access it to force switch to the new API.

king-ma1993 commented 4 months ago

image @levlam I think you should launch support for the new API as soon as possible. I saw in Google's Firebase backend that the old API key will not be usable at all starting from June 20, 2024. So, please update it as soon as possible. Also, will you update support for the new API on https://my.telegram.org at that time?

levlam commented 4 months ago

Yes, there should be changes at https://my.telegram.org/.

levlam commented 4 months ago

I have found that "Starting from 5/20/2024, new projects will no longer be allowed to enable our legacy APIs.", so you need to wait for the support of the new API, which is expected soon.

king-ma1993 commented 4 months ago

@levlam How long will it take to support the new API? Thank you for your support.

levlam commented 4 months ago

It is supposed to be publicly available before June 20.

levlam commented 3 months ago

@king-ma1993 It should be already possible to upload FCM credentials at api.telegram.org to use new push notification API.

ponecrazy commented 3 months ago

@levlam Hi, I see the change, but why does the credentials JSON file returned by Google(Service Account by Firebase Admin SDK) report as invalid when I try to upload it? Do I need to convert it to a different format? Or did I get the wrong file?

levlam commented 3 months ago

@ponecrazy The file should be accepted. Could you edit the JSON file, removing the private key from it, and send the edited file to https://t.me/tdlib_bot?

levlam commented 3 months ago

@ponecrazy Could you try again now?

ponecrazy commented 2 months ago

@levlam Now push is working, previously it was not successful because I changed the appid without relogging in. Thank you for your support.