How do I prevent users from getting their own messages as push notifications?

Modified on: Wed, Jul 18, 2018 at 2:40 PM

When a user sends a message, likely you are publishing that message so that actively subscribed users will receive that message in realtime and also as a mobile push notification (APNS or FCM) so that offline users (that were actively subscribed) get the message as a mobile push message. But if the sender backgrounds the app very quickly after sending the message, it is possible that they will get a push notification for the message they just sent. This is not likely the behavior you intended.

Using the pn_exceptions attribute, you can prevent this from happening. For any message that gets published, create the message payload so that the sending device's push token is added to this key. Notice that each type of push notification has its own pn_exceptions attribute.

If you need to exclude more than just the sender device, you can add as many device push tokens as required. Perhaps a user has multiple devices and you do not want to send the user's message (as a push notification) to any of that user's devices.