0

I'm working on Sony Watch now to implement simple notification whenever user clicks a button from my application(I'm doing this using pub nub). So I'm currently facing an issue of not being able to update the textView with the value from a subsequent notification.

For example: At my intent, I have a 'Done' button and an empty textView. First notification received on Watch (contains number 5), opened notification, at the intent (inside onCreate) I do textview.setText(" " + number) and it shows 5. Now, still at the intent, a new notification arrives (contains number 6) and I've yet to open it. After I click 'Done' and clear number 5 from textView, I open the new notification, it should update the textView to show 6 but the number becomes 0 instead. Why is that so?

The way I'm doing it is supposed to update the textView with value from subsequent notifications every time user is at the intent. But I can't seem to get it to work.

Does anyone know how to do it? Any help is greatly appreciated. Please also provide some advice. Thank you.

I'm also working with Hashmap, below is my code:

NotificationActivity class:

    Button btn = (Button) findViewById(R.id.doneBtn);
    btn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            int[] arr = new int[10]; //a set of numbers to match with my Hash key from pubnub code
            for (int i = 0; i < 10; i++){
                arr[i] = i + 1;

                Test test = new Test();
                if(test.NumberHash.get(arr[i]) != null) { 

                    Log.d("Clearing ", "= " + arr[i]);
                    test.NumberHash.put(arr[i], null);
                    test.NumberHash.clear();
                    TextView number = (TextView) findViewById(R.id.number);
                    number.setText("");                       
                }                
                else{                        
                }
            }
        }
    });
    removeNotification(noID);        

    Test test = new Test();
    TextView number = (TextView) findViewById(R.id.number);              
    onCreateSetText();
}

private void removeNotification(final int notificationId) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(notificationId);
}


private void onCreateSetText(){
    Test test = new Test();
    TextView number = (TextView) findViewById(R.id.number);
    number.setText("" + test.NumberHash.keySet().hashCode());
    Log.d("Set text "," " + number.getText());
    return;
}

Test class:

 private int notificationId = new Random().nextInt(8000) + 1;
    public static HashMap<Integer, String> NumberHash = new HashMap<Integer, String>();

  @Override
public void successCallback(String channel, Object message) {
                        System.out.println("SUBSCRIBE : " + channel + " : "
                                + message.getClass() + " : " + message.toString());

   int Number = Integer.parseInt(message.toString());
              if(NumberHash.get(Number) == null){
                 NumberHash.put(Number, "requested");
                Log.d("number from Test.java"," " + Number);
                //notify
  Intent notificationIntent = new Intent(getBaseContext(),NotificationActivity.class);

  PendingIntent notificationPendingIntent = PendingIntent.getActivity(
               getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);      

  NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                                    R.id.icon_only, null,
                                    notificationPendingIntent).build();

  NotificationCompat.Builder notificationBuilder =
                                    new NotificationCompat.Builder(getBaseContext())
                                            .setSmallIcon(R.drawable.bell)
                                            .setContentTitle("Number is ")
                                            .setContentText(message.toString())
                                            .setVibrate(new long[]{0, 1000, 1000, 0, 0})
                                            .setLights(Color.BLUE, 3000, 3000)
                                            .setAutoCancel(true)
                                            .addAction(action)
                                            .extend(new NotificationCompat.WearableExtender()
                                                    .setContentAction(0));


                            NotificationManagerCompat notificationManager =
                                    NotificationManagerCompat.from(getBaseContext());

                            notificationManager.notify(notificationId, notificationBuilder.build());
                            Log.d("ID", "id = " + notificationId);
                        }

                    }
Sterling
  • 6,365
  • 2
  • 32
  • 40
zwxzwx
  • 37
  • 2
  • 10
  • Your `Activity` code seems to be clipped. What does the `Test` class look like, and how/where is it initialized? – Sterling Jun 06 '16 at 01:09
  • The code in Activity is inside the onCreate method and in my Test class, my successCallback is in a try catch block that processes pub nub. – zwxzwx Jun 06 '16 at 01:49
  • Without seeing more of your code, I'm just guessing here (not to mention, I'd never even heard of "pub nub" before this question). It's possible that your problem lies there, and you might have more success getting help if pub nub has a support forum. Otherwise, I'd guess that the issue is that your `TextView`-updating code is in `onCreate`, which may not be getting called at the times you need it to (read about the Android `Activity` lifecycle). What do you see when stepping through this code in the debugger? – Sterling Jun 06 '16 at 14:03
  • It sounds like you are receiving your message from *PubNub* just fine and it is just an app bug where your variable that is holding the value of the delivered message is being reset to 0. If you have any further evidence that it might be PubNub you can provide it here or contact *PubNub Support*. – Craig Conover Jun 06 '16 at 16:52
  • Hi all, thanks for the replies. I'm now able to update it by creating an array and then storing the values that is coming in from pubnub. Then i just set text to the textview from there. Also, I think that the 0 appears because I've previously cleared the values and that's why it happened. – zwxzwx Jun 07 '16 at 02:51

0 Answers0