0

I have a question, I have read both Make notification disappear after 5 minutes and Clearing notification after a few seconds , but I still don't understand the part where they call removing of the notification. I have a incoming firebase notification coming to my phone and If the user does not click on it,I want the notification removed/disappear automatically after 20 seconds. May I know how to implement it?

P.S I did read on the services as well. I am new to Java language and picking it up as I try a little demo. https://developer.android.com/guide/components/services.html

My codes below are crashing my app everytime i receive a notification. Any help will be appreciated.

Edited: Its fully functional. For anyone's reference

        private void removeNotification()
{
    long delayInMilliseconds = 20000; 
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run()
        {
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.cancel(0);
            timer.cancel();
        }
    }, delayInMilliseconds, 1000);
}
Community
  • 1
  • 1
iOSAndroid
  • 17
  • 1
  • 11

3 Answers3

2

as per i have implemented create new class and extends FirebaseMessagingService

you can write following code to send notification:

 private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("TEST NOTIFICATION")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    } 

for cancel notification create new method and write following code:

Handler h = new Handler();
    long delayInMilliseconds = 20000;
    h.postDelayed(new Runnable() {
        public void run() {
            notificationManager.cancel(id);
        }
    }, delayInMilliseconds);
Sangram Haladkar
  • 707
  • 9
  • 22
0

Yeah, it is very easy. Where you get notification there add one handler if notification is not read by user then remove notification.

@Override
public void onMessageReceived(RemoteMessage message) {
sendNotification(message.getData().toString);
}

add notification code

private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("TEST NOTIFICATION")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int id = 0;
        notificationManager.notify(id, notificationBuilder.build());
        removeNotification(id);
    } 

cancel notification code.

private void removeNotification(int id) {
Handler handler = new Handler();
    long delayInMilliseconds = 20000;
    handler.postDelayed(new Runnable() {
        public void run() {
            notificationManager.cancel(id);
        }
    }, delayInMilliseconds);
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
  • Hi Hulk, thanks for your reply, but for the cancel notification code, in public void run, we have to declare notificationManager again isnt it? if not we will have a unable to resolve symbol error – iOSAndroid Aug 17 '16 at 00:32
  • my App also crashes once my notification is received. I closed the app before i received my notification – iOSAndroid Aug 17 '16 at 01:44
  • Hi Hulk, sorry I cant do the error stack trace, because I am testing on the physical app itself, please refer to the codes above. – iOSAndroid Aug 17 '16 at 04:29
  • Hi Hulk, i solved the issue already, instead of handler, i am using timer to countdown then removing of the notification. thanks for your help! – iOSAndroid Aug 17 '16 at 07:39
0

There is a property called ttl in admin sdk of Firebase-Messaging. See ttl Parameter for Android Config from Firebase

AndroidConfig.ttl: Time-to-live duration of the message in milliseconds.

function createMessage(
    title: string,
  body: string,
    fcmTokens: string[],
): MulticastMessage {
    return {
        notification: {
            title: title,
            body: body,
        },
        android: {
            ttl: 1000 * 60 * 60 * 8, // 8 hours in millis,
        },
        tokens: fcmTokens,
    }
}
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34547075) – user12256545 Jun 20 '23 at 17:11