Reputation: 61
Hi I am working in chat app where if person A sends request and if person B is in idle state i am displaying in notification as you have received a notification from person A and if person B not responded for 60 seconds,I clear the notification and call missed notification. But I need to display total number of missed chats notifcation if in idle state and if user see the missed chat and clears the notification and if next notification comes, it should display count as 1 similar to phone missed call. How to proceed?Here is code which I done. I need to display count in notification.
Here is code which I tried.
notBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationID, notBuilder.build());
clearPendingNotification(notificationID);
}
private void clearPendingNotification(final int id) {
try {
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mManager.cancel(id);
missedNotification("test");
}
}, Constants.CHAT_REQ_TIME);
} catch (Exception e) {
LogMessage.e(Constants.TAG, e);
}
private void missedNotification(String message) {
Intent notificationIntent = null;
Context context = getApplicationContext();
int notificationID = new Random().nextInt();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(
context);
notBuilder.setSmallIcon(R.drawable.ic_notification);
notBuilder.setContentTitle("Missed chat request");
notBuilder.setContentText("You have" + " " + message + " "
+ "missed chat request from customers");
notBuilder.setAutoCancel(true);
notBuilder.setDefaults(Notification.DEFAULT_SOUND);
notificationIntent = new Intent(context, ActivityExpertDasboard.class);
notificationIntent.putExtra(Constants.IS_CHAT_REQUEST, true);
notificationIntent
.putExtra(Constants.KEY_NOFICATION_ID, notificationID);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationIntent.putExtra(Constants.CHAT_USER_NAME, msgTo);
PendingIntent pendingIntent = PendingIntent
.getActivity(context, notificationID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
notBuilder.setContentIntent(pendingIntent);
notificationManager.notify(notificationID, notBuilder.build());
}
}
Thanks in advance.
Upvotes: 1
Views: 251
Reputation: 6973
Adding to @Karakuri's answer you also need to check that the Activity is Visible means its in foreground. Since you'll be receiving notification from GCM, you need to open connection to that service and let service know that activity is visible. When activity is Visible do not post notification instead pass data from service to activity or insert silently in db and send some broadcast to activity indicating that new data has been updated in db please query for latest messages.
Upvotes: 2
Reputation: 38605
Store the number of missed notifications somewhere (like SharedPreferences
). You can keep incrementing this value until the user clicks on (or swipes away) a notification. Use setDeleteIntent()
to call a component that will reset this stored value when the notification is canceled.
Upvotes: 1