I've a requirement, wherein the received GCM push notification should get auto canceled/destroyed if not viewed for 2 hours. The app may or maynot be in background. Is making services the best way to deal with this? Any other suggestions to handle this?
Asked
Active
Viewed 1,680 times
2
-
is it similar question to http://stackoverflow.com/questions/3479428/android-notification-intent-to-clear-it-self – Orbitcoder Jan 03 '15 at 15:56
-
1No, it says he wants to clear notification "when clicked". My client's requirement is to clear it "without touching" if the notification was not viewed or touched for 2hrs. – Prachi Jan 04 '15 at 03:18
3 Answers
0
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancel(NOTIF_ID);
}
}, 600000);

Divya SIngh
- 129
- 1
- 11
0
Yes.Service is the best solution for reliable update and works background as well.
1.Maintain the local temporary Table(DTO),there save all incoming notification and should add field notification arrive time.
2.In service class compare with current time and notification time,if its exceed more than 2 hours delete notification item from table and cancel notification.
Here is Code
public class Notyficationclr extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
for(Notification not:NotifcationTable.getAll()){
if(IntervalOver(not)){
//Code for delete Notification from table
//code for cancel notification
}
}
public boolean IntervalOver(Notification notification) {
boolean istimeout = false;
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat hformat = new SimpleDateFormat("HH");
SimpleDateFormat mformat = new SimpleDateFormat("mm");
String hour = null, min = null;
Date arrivatedatetime = null;
try {
arrivatedatetime = sdf.parse(notification.getArrivalTime());
} catch (java.text.ParseException e) {
e.printStackTrace();
}
Date currentDate = new Date(System.currentTimeMillis());
long diff = currentDate.getTime() - arrivatedatetime.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
if (hours >=2) {
istimeout = true;
}
return istimeout;
}

Rajith Rajan
- 121
- 7