iOSAndroid
iOSAndroid

Reputation: 17

Auto Remove Firebase Notifications

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);
}

Upvotes: 0

Views: 6834

Answers (3)

Jonah Sebright
Jonah Sebright

Reputation: 1

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,
    }
}

Upvotes: 0

Yogesh Rathi
Yogesh Rathi

Reputation: 6509

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);
}

Upvotes: 0

Sangram Haladkar
Sangram Haladkar

Reputation: 717

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);

Upvotes: 2

Related Questions