Python : Message.token must be a non-empty string

1,433 views
Skip to first unread message

Mael Elvis FOSSO N.

unread,
Nov 13, 2019, 11:06:00 AM11/13/19
to Firebase Google Group
Hello 

I have a problem when trying to send a notification to an user. I don't know why but I got Message.token must be a non-empty string

Could you help me to discover the problem ?

Here is my code.

app = get_app()
driver_uuid = self.validated_data['driver'].user.id

registration_token = auth.create_custom_token(str(driver_uuid))
message = messaging.Message(
            notification = messaging.Notification(
                body = json.dumps({
                    "status""test"
                }),
                title = "Title"
            ),
            data = {
                'score''850',
                'time''2:45',
            },
            token=registration_token
        )

try:
            response = messaging.send(message, app=app)
            # Response is a message ID string.
            print('\nSuccessfully sent message:', response)
        except Exception as e:
            print('\nEXCEPTION... EX \n')
            print(e)

        except messaging.ApiCallError as ex:
            response = ex.detail.response # This is a requests.Response object
            response_data = response.text # Read the response as a string
            print('\nEXCEPTION API CALL ERROR ...\n')
            print('\t', reponse, '\n')
            print('\n\t', response_data, '\n')


# The registration token
eyJ0eXAiOiAiSldUIiwgImFsZyI6ICJSUzI1NiIsICJraWQiOiAiMDg4ZjE4ODkwZmNiNzE3MjI0ZmM5YzljOWFjNDA1NWEwZWI3YzBkYSJ9.eyJpc3MiOiAiZmlyZWJhc2UtYWRtaW5zZGstd2hxajhAdGF4aS05ODk1Ny5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsICJzdWIiOiAiZmlyZWJhc2UtYWRtaW5zZGstd2hxajhAdGF4aS05ODk1Ny5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsICJhdWQiOiAiaHR0cHM6Ly9pZGVudGl0eXRvb2xraXQuZ29vZ2xlYXBpcy5jb20vZ29vZ2xlLmlkZW50aXR5LmlkZW50aXR5dG9vbGtpdC52MS5JZGVudGl0eVRvb2xraXQiLCAidWlkIjogImYxOTkwZWZjLTBkNmItNDEyOC1hNThiLWQ0ODFkMTgxYmRhYyIsICJpYXQiOiAxNTczNjU4ODA3LCAiZXhwIjogMTU3MzY2MjQwN30.CB9CRJ0DsE8ccktbbwPOaG2WOaTEHmER790OTQi02d8Cy0wH3V-qrfwVeshG5TprmoPE0p1-d88Z_fiYIwM1AQvuRLW5G0xsGSjBVE-gBqgtUhiDf-f0ZmdzCuSVF2o-dFeKcLU6hQKca6DLh70XnwxgnhD4YIaTQU-QkzNmhfc8X_l0dSQzjiqVk6rSuxRHP_lo-0YkkFF7VdugOga9k6Q3TWfbc5AtAavkruzSnLj8urXtpAzftrFFMLgAusMxP4IgYDAIm7J1YUB0-5iVsf8YInsb2rijV83KXqJg5_Tj_r0cN1SgdRya22i6YTEFm47thJ8BYnEy9PiPox3_nA

Hiranya Jayathilaka

unread,
Nov 13, 2019, 1:30:50 PM11/13/19
to fireba...@googlegroups.com
Firebase custom auth tokens cannot be used to send FCM notifications. For that you need a FCM device registration token generated on a client device. See documentation on how to generate one. For example on an Android device: https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token

In any case the above error is probably because auth.create_custom_token() returns a raw bytes object, while Message expects an str.

Thanks,
Hiranya

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/b7c359e0-3cba-4a78-8b43-4abb07693610%40googlegroups.com.


--

Hiranya Jayathilaka | Software Engineer | h...@google.com | 650-203-0128

Mael Elvis FOSSO N.

unread,
Nov 14, 2019, 10:53:22 AM11/14/19
to Firebase Google Group
Thank you


Le mercredi 13 novembre 2019 19:30:50 UTC+1, Hiranya Jayathilaka a écrit :
Firebase custom auth tokens cannot be used to send FCM notifications. For that you need a FCM device registration token generated on a client device. See documentation on how to generate one. For example on an Android device: https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token

In any case the above error is probably because auth.create_custom_token() returns a raw bytes object, while Message expects an str.

Thanks,
Hiranya

To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

Mael Elvis FOSSO N.

unread,
Nov 14, 2019, 5:32:43 PM11/14/19
to Firebase Google Group
Thanks for the solution.

However there is something I don't understand.

What is the connection between signInWithCustomToken and getToken ?
How are there connected ?

I mean:
  • In the server, when the user sign in with his password and username, I create the firebase custom token which I send to him and he connects using signInWithCustomToken
  • What I understand from your answer is, when the user launch the app we retreive the token using getToken and then save it in the server. Everytime, we want to send a notification to that user, we use the token send in the server
My question now is : 
What is the connection between the answer from signInWithCustomToken and the token get from getToken ?
I was supposing that the answer from signInWithCustomToken will be used for generating the token.


Could you clarify me please ?



Le mercredi 13 novembre 2019 19:30:50 UTC+1, Hiranya Jayathilaka a écrit :
Firebase custom auth tokens cannot be used to send FCM notifications. For that you need a FCM device registration token generated on a client device. See documentation on how to generate one. For example on an Android device: https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token

In any case the above error is probably because auth.create_custom_token() returns a raw bytes object, while Message expects an str.

Thanks,
Hiranya

To unsubscribe from this group and stop receiving emails from it, send an email to fireba...@googlegroups.com.

Kato Richardson

unread,
Nov 17, 2019, 11:56:16 AM11/17/19
to Firebase Google Group
Authentication and Cloud Messaging do not share tokens. These are separate products.

The firebase.messaging.Messaging.getToken() method is for FCM, and we commonly refer to this as the "messaging registration token". You can read about this here.

The firebase.auth.Auth.signInWithCustomToken() is used for bring-your-own-auth solutions, where you maintain a database of user credentials and sign tokens for your users on behalf of Firebase; this is completely unrelated to FCM messages. You can read about this here.

☼, Kato

To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/1a87c30a-b153-4d02-a257-086c7618b4cd%40googlegroups.com.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages