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);
}
}