vendredi 8 mai 2015

Android - Update status bar notification

I have created a android application, that polls the server once in two minutes by making use of the Alarm service. After the alarm triggers a Service Intent, that will poll the server for any new data. If new data is available, a status bar notification will be generated using NotificationManager class.

I have few queries here:

  1. At first a notification will be generated indicating new data update arrived from server. After two minutes, again new data is available. How to know whether user clicked the old notification or not? By knowing I will decide whether to put "New message arrived" or "2 New messages arrived"

  2. How can I update the existing notification from the service intent itself?

  3. How can I get the extras and append it in the listview of the Activity? Now each time I click the notification, new listview is created in the Activity and only that message is appended.

PollingServer.class:

            NotificationManagerCompat.from(this).cancelAll();

            Intent notificationIntent= new Intent(this, Messages.class);
            notificationIntent.putExtra("msg",toastText);

            TaskStackBuilder stackBuilder=TaskStackBuilder.create(this);
            stackBuilder.addParentStack(Messages.class);
            stackBuilder.addNextIntent(notificationIntent);

            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.clove)
                            .setContentTitle("My notification")
                            .setContentText(toastText)
                            .setContentIntent(contentIntent)
                            .setDefaults(Notification.DEFAULT_SOUND)
                            .setAutoCancel(true);

            NotificationManager mNotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());

Messages.class:

            private ListView mainListView;

            @Override
            protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_messages);
            Bundle extras = getIntent().getExtras();
            final String msg = extras.getString("msg");

            // Find the listView Resource
            mainListView = (ListView) findViewById(R.id.listview);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1);
            mainListView.setAdapter(arrayAdapter);
            arrayAdapter.add(msg);

         }

Aucun commentaire:

Enregistrer un commentaire