Archie.bpgc
Archie.bpgc

Reputation: 24012

Android: Notifications using a Service

I never worked with Services before. So, after following few demons on internet my implementation is this:

In my MainActivity's onResume() I am starting the Service this way:

protected void onResume() {
super.onResume();
startService(new Intent(MainActivity.this, NotificationsService.class));
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, NotificationsService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000, 60000, pi);
}

And my NotificationsService Class is:

public class NotificationsService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handleIntent(intent);
        return START_NOT_STICKY;
    }
    private NotificationManager nm;
    private WakeLock mWakeLock;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWakeLock.release();
    }

    private void showNotification() {

        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.icon,
                "Notification Ticker", System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Date date = new Date(System.currentTimeMillis());
        Intent i = new Intent(this, NotificationsActivity.class);
        i.putExtra("notification",
                "This is the Notification " + date);
        i.putExtra("notifiedby", "xyz");
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i,
                PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(this, "xyz",
                "This is the Notification", contentIntent);
        nm.notify(R.string.service_started, notification);
    }

    private class PollTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            showNotification();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            stopSelf();
        }
    }

    private void handleIntent(Intent intent) {
        // obtain the wake lock
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "NotificationsService");
        mWakeLock.acquire();
        // check the global background data setting
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        if (!cm.getBackgroundDataSetting()) {
            stopSelf();
            return;
        }
        new PollTask().execute();
    }
}

In the NotificationsActivity I am getting the Extras and showing the. These Extras have a time stamp and I am calling the showNotifications() method once every minute (60000 ms).

Problem:

  1. The time stamp I am showing in the NotificationsActivity, which I get from the Service Extras is the TimeStamp of the 1st Notification

E.g., if it's 10:10:10 A.M for the 1st notification it's been always 10:10:10 A.M in the Activity. But in the Notifications panel it's showing the updated one like 10:15:10 A.M for every Notification created every minute.

  1. If I am setting the Notification every minute, I expect the Notifications to be separate. Instead it is just replacing the previous Notification. Or best would be like 10 Notifications from myApp.

How to get these?

Mainly I want to know why the timestamp not getting updated?

Upvotes: 0

Views: 439

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

The time stamp i am showing in the NotificationsActivity, which i get from the Service Extras i the TimeStamp of the 1st Notification

If the activity was still running, it should be called with onNewIntent() instead of onCreate(), and the Intent delivered to onNewIntent() should have the appropriate extras. getIntent() always returns the Intent used to create the activity in the first place.

If i am setting the Notification Every minute, I expect the Notifications to be separate.

Users think that developers who flood their screen with notifications to be complete imbeciles.

Instead it is just replacing the previous Notification.

That is what you are telling it to do with your call to notify().

Upvotes: 1

Related Questions