jazzband / django-push-notifications

Send push notifications to mobile devices through GCM or APNS in Django.
MIT License
2.28k stars 618 forks source link

Firebase iOS notifications not working with new HTTP v1 API #723

Open sstuart-vigil opened 6 months ago

sstuart-vigil commented 6 months ago

I moved my app to use Firebase instead of APNS for iOS devices, and it was working with the legacy FCM API. After updating to the new HTTP v1 API notifications no longer go to iOS devices. Android devices work as expected.

When sending from the Django admin I get an 'All messages were sent.' success message, and when sending from the command line using send_message I get a success response as well.

I took the device registration ID for my iOS test device and sent a message using the firebase console and the notification worked. So the configuration is correct on that side, is there anything additionally that needs to be done in Django?

pupubird commented 6 months ago

Same here, works on android but not ios

Klemenceo commented 6 months ago

The way it gets wrapped in message is wrong now, one way to circumvent that is wrap this in a message directly as the send message special cases that type. Something like this : m = messaging.Message(notification=messaging.Notification(title="Title", body="body")) should work both on iOS and Android.

note: If you need to send high volumes of message the HTTP v1 API is slow as there is no batching endpoint, and the firebase SDK doesn't use HTTP/2 yet.

irvinsingGitHub commented 3 months ago

@Klemenceo thanks for the tip. Tried your method and it worked with:

from firebase_admin import messaging

device.send_message( messaging.Message( data={'data_1': 'my_data_1'}, notification=messaging.Notification(title='my title', body='my message',), ) )

However, I'm also trying to add 'badge' and 'sound' for ios. Where would you include them using this method?

g-normand commented 3 months ago

Hello,

I used to send like this:

attached_device.send_message(self.message_to_display, extra=self.payload)

and it's not working anymore. With @irvinsingGitHub code, it's working this way :

attached_device.send_message(messaging.Message(
       data=self.payload, 
       notification=messaging.Notification(body=self.message_to_display)))

My problem is that I used to do some unit test:

payload = json.dumps({
     'extra_session': {'id': session.id, 'match_type': RUNNING},
     'click_action': 'OPEN_SESSION_DETAILS_ACTIVITY',
     'click_action_id': str(session.id),
      'message': message,
})
notification_mock.assert_called_with(message, extra={'payload':payload})

but now I can't run the unit test with messaging.Message, does anyone know how I can do that?

Thanks

DanielssonP commented 2 months ago

To send a data message to a topic in FCM, I had to do:

    m = messaging.Message(
        data={
            "my_data": "data"
        },
        android=messaging.AndroidConfig(
            priority="high"
        ),
        topic="/topics/my_topic"
    )
messaging.send(m)

I may have misunderstood, but as a sidenote, there also seems to be a spelling mistake in the code (or in the docs). Docs say to="/topics/my_topic" while dict_to_fcm_message() is looking for to.startswith("/topic/"), with a missing s at the end.