1

I'm building a chat feature on iOS. I'm using PubNub's publish/subscribe system for sending messages between clients.

Problem:

Whenever I publish to a channel that I'm subscribed to, I also get a push notification of the message I sent. Although, I can simply ignore this when the app is in the foreground, the problem arises when the notification is delayed and the app goes to the background.

I would like to only receive push notifications when other people send messages to the channel. It doesn't make sense to get notified that you sent a message.

A solution I'm considering is creating additional channels just for push notifications but I find that inefficient and a bit hacky. Please tell me there's another solution.

Jay Q.
  • 4,979
  • 3
  • 33
  • 36

2 Answers2

2

PubNub Sender Identification Implementation

The best solution is to use silent push notifications and include the sender’s UUID in the message payload. Here's an example:

{
    'pn_apns': {
        'aps': {
            'content-available':1
        },
        'sender_id':'user123',
        'content':'this could be more key/values that you can process on the device'
    },
    'data': 'realtime key/values goes here'
}

Then in the silent push processing on the device, you can do:

if uuid != my_uuid then display msg; else do nothing

You can use our Badge Count Demo app that uses silent push notifications to fetch history when the silent push notification is received on the device to calculate accurate badge counts. Just replace the history fetch code with the is this from me code.

You should also review Configuring a Silent Notification Apple Documentation for full details about how silent push works.

Android push notifications are silent by default and you have to explicitly display them so same can work on that platform with GCM, if required.

There will be a more elegant solution soon but until those enhancements are rolled out, the above should provide you what you need.

Community
  • 1
  • 1
Craig Conover
  • 4,710
  • 34
  • 59
  • Hi Craig! Is this still the best approach, or is there any other newly available workaround? I am sending messages from iOS 10 to iOS 11. If sending with the alert entry iOS 11 shows correctly the alert, but the sender as well. If using your suggestion it works fine on sender, but receiver, since it is iOS 11 it is not consistently getting the notification. I think it has to do with this issue on iOS 11 [Stackoverflow](https://stackoverflow.com/questions/44796613/silent-pushes-not-delivered-to-the-app-on-ios-11) and [Apple](https://forums.developer.apple.com/thread/79734). Thank you! – Tamara Bernad Jul 13 '18 at 13:26
  • Actually it is working for Native iOS, since the PushNotifications are not shown when app is on foreground as the [documentation](https://www.pubnub.com/docs/phonegap-javascript/mobile-gateway#about_the_pubnub_mobile_push_gateway) specifies (unless you actually specify `content-available`, then the notification is always shown). Problem appears in React Native, where the Notifications are shown no matter if the app is in foreground or not. This might need to be a separate question. – Tamara Bernad Jul 13 '18 at 15:21
  • If you move to Apple's new APNS2 Push Tokens (which we will release very soon to the public) you can add a [pn_exceptions key](https://www.pubnub.com/docs/web-javascript/mobile-gateway#device-exceptions) with an array of device push tokens to exclude from receiving the push notification. So, of course, just always include the sending device's push token. There will be an announcement soon. – Craig Conover Jul 14 '18 at 23:28
  • 1
    That is great news, thank you so much for answering so quickly! We found the reason why the notification is actually being displayed meanwhile the app is in foreground. It is due to another library we are using in the app as well, "OneSignal" – Tamara Bernad Jul 15 '18 at 14:42
  • Stay tuned for a new answer to this post about the new forthcoming APNS2 Push Token features. Lots of good stuff will be announced soon. – Craig Conover Jul 15 '18 at 18:30
1

Using pn_exceptions to exclude devices [APNS2]

Recently, the PubNub mobile push gateway has been updated to use APNS2. With this update you can now use pn_exceptions to exclude certain device tokens from the push notifications.

Here's how the updated payload would look.

{
    "pn_apns" : {
        "aps" : {
            "alert" : {
                "body" : "hello (via APNS)"
            }
        },
        "pn_exceptions" : [
            currentDeviceToken
        ]
    },
    "pn_gcm" : {
        "alert" : "hello (via FCM)",
        "pn_exceptions" : [
            someDeviceToken
        ]
    },
    text : "hello (in realtime)"
}
Syed Ahmed
  • 21
  • 3