Send a push notification only to particular device

I want to send a push notification only to a specific device registered but the example in link : https://backendless.com/docs/android/push_target_specific_devices.html uses addPushSinglecast method of DeliveryOptions class which is not available for Backendless version 5.2.4 whereas setPushSinglecast method of DeliveryOptions is also not helping as it sends notification to all devices registered in the channel
My App ID:
210EDE09-C21A-5E81-FFA7-1D7CDC970500

Hello @Prashant_Kain

You use Android SDK?
Write a piece of your code here so that I can reproduce.

Hi @Volodymyr_Ialovyi
Yes I use Android SDK (Android Studio)
Here is my code where I am using setPushSingleCast :

DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.setPushSinglecast(Arrays.asList(devId.get(0)));
publishOptions.putHeader("android-content-title",user);
publishOptions.putHeader("android-content-text",message);
Backendless.Messaging.publish("ChitChatChannel", message,
        publishOptions, deliveryOptions, new AsyncCallback<MessageStatus>() {
            @Override
            public void handleResponse(MessageStatus response) {
                messages_tv.setText("You : " + message + "\n" + messages_tv.getText());
            }
            @Override
            public void handleFault(BackendlessFault fault) {}
        });

Please send the entire file to support@backendless.com, this code is not enough to reproduce the problem

Hey @Prashant_Kain

Have just tried your code and everything works as expected:

List<String> devices = new ArrayList<>();
devices.add( "MY_DEVICE_ID" );
DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.setPushSinglecast( devices );

PublishOptions publishOptions = new PublishOptions();
publishOptions.putHeader( "android-ticker-text", "You just got a private push notification!" );
publishOptions.putHeader( "android-content-title", "Backendless Test" );
publishOptions.putHeader( "android-content-text", "Targeted message (single cast)" );

Backendless.Messaging.publish( channelName, "This is a private message!", publishOptions, deliveryOptions, new AsyncCallback<MessageStatus>()
    {
      @Override
      public void handleResponse( MessageStatus messageStatus )
      {
        System.out.println("Message status: " + messageStatus.getStatus() + "\n");
        System.out.println("Message ID: " + messageStatus.getMessageId());

  }

  @Override
  public void handleFault( BackendlessFault backendlessFault )
  {
    System.out.println("Error: " + backendlessFault.getDetail());
  }
} );

I receive a push only on device i specified in request. Please debug your project and make sure that your are passing an array with only 1 deviceId in setPushSinglecast() method

Anton

Thanks for helping me but i want to ask how can i get deviceId of my friend’s device,so that i can send push notification only to him.
My “DeviceRegistration” Table has column named “user” having 1:1 relation pointing to “Users” Table having column named “name”.
I write code as :

  final String[] devId = {new String()};
  DataQueryBuilder dataQueryBuilder=DataQueryBuilder.create();
  dataQueryBuilder.addRelated("user");
  dataQueryBuilder.setWhereClause("user.name = '"+friend_name+"'");
  Backendless.Data.of("DeviceRegistration").find(dataQueryBuilder, new 
                         AsyncCallback<List<Map>>() {
                        @Override
                        public void handleResponse(List<Map> response) {
                            if(!response.isEmpty())
                            {
                                Map friendId = response.get(0);
                                devId[0] =(String) friendId.get("deviceId");
                            }
                        }
                        @Override
                        public void handleFault(BackendlessFault fault) { }
                    });

But it is not working.

Hello!

Please give your app ID, or is this same application?

Hi, Denys.
Yes,its the same application as mentioned above.
My App ID:
210EDE09-C21A-5E81-FFA7-1D7CDC970500

Hi, Prashant!

You’re saying you’ve got table relations, but I don’t see those relations in your app

Denys

Hey Denis,
Please Check again

@Prashant_Kain Hi! Have you read https://backendless.com/docs/android/data_general_api.html?
Look at syntax of
Backendless.Data.of( "TABLE-NAME" ).find( DataQueryBuilder query, AsyncCallback<List<Map>> );
and here:
TABLE-NAME - Name of the table to retrieve data from.
So you are trying to retrieve data from DeviceRegistration table. But your table is empty.

Please, make sure that your query and request are correct.

Hi Sofia,
I made Login based Messaging App.
My DeviceRegistration table fills when i login into my app and delete the entry in table after log out.And i am sure using the code as above, and at the time when the table has required entry.
Thanks

Somebody, Please help I have to complete my project in 2-3 days.
Thanks in advance.

Hi @Prashant_Kain,

Please be very specific in describe what problem you are experiencing. Saying “it is not working” does not help. We need to be able to reproduce the problem. It means you need to provide us with a sample which clearly demonstrates the problem.

Cheers,
Mark

Hi Mark,
I made a messaging app which allows multiple users to log in, in which they can send messages to their friend (Friends are stored in Friends table). Now on selecting a friend from the list in-app, I got the name of a friend which friend name has been clicked. And at the time of login, I registered a device, in DeviceRegistration table. Now I want to search the device ID of that friend (which was selected) from DeviceRegistration Table which has a column named “user” having 1:1 relation pointing to “Users” table.
I used this code :

final String[] devId = {new String()};
DataQueryBuilder dataQueryBuilder=DataQueryBuilder.create();
dataQueryBuilder.addRelated(“user”);
dataQueryBuilder.setWhereClause(“user.name = '”+friend_name+"’");
Backendless.Data.of(“DeviceRegistration”).find(dataQueryBuilder, new
AsyncCallback<List>() {
@Override
public void handleResponse(List response) {
if(!response.isEmpty())
{
Map friendId = response.get(0);
devId[0] =(String) friendId.get(“deviceId”);
}
}
@Override
public void handleFault(BackendlessFault fault) { }
});

But it didnt help me, which means my friend is not getting any push notification.
And I am sure my friend’s device is registered at the time when I am sending him a message.
Thanks.

1 Like