0

Not receiving the first Notification

asked 2014-11-14 03:38:26 -0800

almcalle gravatar image

updated 2014-11-14 03:42:47 -0800

NotificationConsumer is not able the receive the first Notification, so I have to send it twice to execute MyNotificationReceiver.receive.

How can I fix it to be able to receive the notification sending it only once?

I had my code:

  public class NotificationConsumer {
static{  
    System.loadLibrary("alljoyn_java");
}

/** The m bus. */
private static BusAttachment mBus;

/** The about service. */
static AboutService aboutService;

/** The notification service. */
static NotificationService notificationService;

/** The notification receiver. */
static MyNotificationReceiver notificationReceiver;

/** The m object. */
static MyBusObject mObject;

/** The port. */
static short PORT=91;

/** The session id. */
static int sessionId;

/** The sesion. */
static boolean sesion = false;





/**
 * The Class MyNotificationReceiver.
 */
public static class MyNotificationReceiver implements NotificationReceiver
{

    /* (non-Javadoc)
     * @see org.alljoyn.ns.NotificationReceiver#dismiss(int, java.util.UUID)
     */
    @Override
    public void dismiss(int msgId, UUID appId) {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see org.alljoyn.ns.NotificationReceiver#receive(org.alljoyn.ns.Notification)
     */
    @Override
    public void receive(Notification notification) {
        // TODO Auto-generated method stub


        System.out.println("Received NOTIFICATION");   

        System.out.println("Name: "+notification.getAppName());
        List<NotificationText> list= notification.getText();
        NotificationText not=list.get(0);
        System.out.println("TEXT: "+not.getText());

    }



}




/**
 * The main method.
 *
 * @param args the arguments
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {



    mBus = new BusAttachment("com.almcalle.Notification",BusAttachment.RemoteMessage.Receive);
    mObject=new MyBusObject();



    //mBus.registerBusObject(mObject,"/almcalle/Notification");

    mBus.connect();


    CreateSessionListener();





    int flags=0;
    mBus.requestName("com.almcalle.Notification", flags);




    Status status = mBus.connect();
    if ( status != Status. OK ) {

    }





    aboutService = AboutServiceImpl.getInstance();

    aboutService.startAboutClient(mBus);

    notificationService = NotificationService.getInstance();





    aboutService.addAnnouncementHandler(mObject);





    notificationReceiver=new MyNotificationReceiver();

    notificationService.initReceive(mBus, notificationReceiver);
    mBus.addMatch("sessionless='t',type='error'");



    while (true) {
        try {
            Thread.sleep(10000);
            mBus.advertiseName("com.almcalle.Notification", SessionOpts.TRANSPORT_ANY);
        } catch (InterruptedException e) {
            System.out.println("Thread Exception caught");
            e.printStackTrace();
        }


    }








}

/**
 * Creates the session listener.
 */
private static void CreateSessionListener() {
    // TODO Auto-generated method stub



    Mutable.ShortValue sessionPort=new Mutable.ShortValue(PORT);
    SessionOpts opts= new SessionOpts();
    opts.isMultipoint=false;
    opts.traffic = SessionOpts.TRAFFIC_MESSAGES;
    opts.isMultipoint = false;
    opts.proximity = SessionOpts.PROXIMITY_ANY;
    opts.transports = SessionOpts.TRANSPORT_ANY;




    mBus.bindSessionPort(sessionPort, opts, new SessionPortListener() {
        public boolean acceptSessionJoiner(short sessionPort, String joiner, SessionOpts sessionOpts) {
            System.out.println("SessionPortListener.acceptSessionJoiner called");
            if (sessionPort == PORT) {
                return true;
            } else {
                return false;
            }
        }
        public void sessionJoined(short sessionPort, int id, String joiner) {
            System.out.println(String.format("SessionPortListener.sessionJoined(%d, %d, %s)", sessionPort, id, joiner));
            sessionId = id;
            sesion = true;
        }
    });



}

}

And here is The notification Producer:

 public class NotSender {
static{  
    System.loadLibrary("alljoyn_java");
}

/** The m bus. */
static BusAttachment mBus;

/** The about store. */
static AboutStore aboutStore;

/** The about service. */
static AboutService aboutService;

/** The notification sender. */
static NotificationSender notificationSender;

/** The sesion. */
static Boolean sesion=false;

/** The port. */
static short PORT=91;

/** The notification service. */
static NotificationService notificationService;

/**
 * The main method.
 *
 * @param args the arguments
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {



    mBus = new BusAttachment("com.almcalle.Notification",BusAttachment.RemoteMessage.Receive);



    RegisterBusListener();



    Status status = mBus.connect();
    if (Status.OK != status) {
        System.out.println("BusAttachment.connect() failed:" + status);
        System.exit(0);
        return;
    }

    CreateStore();
    aboutService = AboutServiceImpl.getInstance();

    try {
        aboutService.startAboutServer((short)1080, aboutStore, mBus);
    }
    catch (Exception e) {

    }
    ////////end about
    mBus.findAdvertisedName("com.almcalle ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-11-17 00:27:56 -0800

almcalle gravatar image

I found how to do it. After it sends the notification I must write Thread.sleep(1000); I now all the program works perfectly

edit flag offensive delete publish link more
Login/Signup to Answer

Question Tools

Follow
1 follower

Stats

Asked: 2014-11-14 03:38:26 -0800

Seen: 74 times

Last updated: Nov 17 '14