Home > database >  Firebase Cloud Messaging - configure sound and vibration
Firebase Cloud Messaging - configure sound and vibration

Time:04-06

Building Rest Api for mobile app. In app user can enable/disable sound and vibration. I need to find out a way to disable them in firebase cloud messaging using firebase-admin package.

The problem is - Firebase provides 'sound' parameter which only takes filename of the sound resource. I dont see any options to disable it. Also, there are no any vibration parameters.

I expect to make code like:

user_settings = UserSettings.objects.get(user=user)
notification = Notification(sound=user_settings.allow_sound, vibration=user_settings.allow_vibration, text=text, title=title)

Is it event possible to send silent notification?

Update

sound = 'default' if user.user_settings.notify_sound else ''
if user.user_settings.notify_vibration:
  vibration_kwargs = {'default_vibrate_timings': True}
else:
  vibration_kwargs = {'vibrate_timings_millis': [0, 0, 0]}
  # Custom timing to off vibration. According to documentation 
  # this array will produce zero duration time for vibration 
android = AndroidConfig(notification=AndroidNotification(sound=sound, **vibration_kwargs))
ios = APNSConfig(payload=APNSPayload(aps=Aps(sound=sound)))
notification = Notification(title='Some title', body='Some body')

message = Message(notification=notification, android=android, apns=ios)
# Then i send this message using fcm-django package 

According to lots if info, this code may help. But i dont test it yet

CodePudding user response:

Code above doesnt work. We cant turn off vibration, usually firebase uses system wire options to manage this. Sound can be turn off:

Apple - send empty string in "sound" parameter Android - send non-exist filename

  • Related