segmentQuery does not work (received on all devices)

Hi

I want to send a push notification to a specific user’s devices. Here my code:

let publishOptions = PublishOptions()
let message = NSLocalizedString("You have bee requested to join a new team", comment: "")
publishOptions.addHeader(name: "ios-sound", value: "alerta.caf")
publishOptions.addHeader(name: "ios-alert-body", value: message)
publishOptions.addHeader(name: "ios-alert", value:  "Alert message")
publishOptions.addHeader(name: "ios-badge", value:  0)
let deliveryOptions = DeliveryOptions()
deliveryOptions.pushBroadcast = PushBroadcastEnum.FOR_ALL.rawValue
deliveryOptions.publishPolicy = PublishPolicyEnum.BOTH.rawValue
deliveryOptions.segmentQuery = "user.objectId = '\(RecID_DTM)'"

Backendless.shared.messaging.publish(channelName: "default", message: message, publishOptions: publishOptions, deliveryOptions: deliveryOptions, responseHandler: { messageStatus in
                            print("Message status: \(messageStatus)")
                        }, errorHandler: { fault in
                            print("Error: \(fault.message ?? "")")
                        })

The notification arrives on to all devices registered to:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

if the app is on the background the notification alert card it does not show.

However, if I send the notification from the console it does work as expected.

My App id is: 02B9EC69-EECD-7DC3-FF4B-E48D8CAA4800

Thank you in advance for your help

Hi Alejandro,

Could you try using the pushWithTemplate API instead? I noticed the documentation doesn’t show the right signature for that, however, if you go to Messaging > Push Notifications > Push Templates section, you will see your templates. Click the Show Code icon:

In the popup that shows, click Swift:

This way the segment condition you identified in the template will be used when sending out the notification.

Regards,
Mark

Hi Mark:

Thank you for this prompt answer.

I tried that way but, my problem is that the Segment where clause that includes the targeted user.objectid on the template is a fix id, and it should be a parameter as it is in the API

deliveryOptions.segmentQuery = "user.objectId = '\(RecID_DTM))'"

:frowning:

regards

I see. Since the user has a related object in the DeviceRegistration table, you should be able to retrieve deviceId for their device. Once you know it, you could try using the following API:

Could you please give it a try and let me know if it works for you?

Regards,
Mark

Thank you for your message.

It does work for me, and the notification arrives now only to the appropriate device. And I can capture the notification on

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

Thank you so much for your continuos support.

However, I continuos having troubles to have the notification on the screen when the app is on the background.

As mentioned earlier, the notification works perfectly using the console.

I am glad it worked out for you, Alejandro.

@olhadanylova, could you please advise what needs to be done in the code so that notifications show up when the app is running in the background?

Regards,
Mark

1 Like

Hello @Alejandro_Cuartas_Fernandez,

please try this payload:

let message = NSLocalizedString("You have bee requested to join a new team", comment: "")
        
let publishOptions = PublishOptions()
publishOptions.headers = ["ios-sound": "alerta.caf",
                          "ios-alert-body": message,
                          "ios-alert-title": "Title",
                          "ios-alert-subtitle": "Subtitle",
                          "ios-badge": 0]

let deliveryOptions = DeliveryOptions()
deliveryOptions.publishPolicy = PublishPolicyEnum.BOTH.rawValue
        
Backendless.shared.messaging.publish(channelName: "default", message: message, publishOptions: publishOptions, deliveryOptions: deliveryOptions, responseHandler: { messageStatus in
	print("Message status: \(messageStatus)")
}, errorHandler: { fault in
	print("Error: \(fault.message ?? "")")
})

Also please notice, that the ios-alert-body header is the same as ios-alert so there’s no need to use both.

Regards,
Olha

1 Like

It does work, thank you again

Best regards