简体   繁体   中英

Android - How to bring notification tray for few seconds in front

I have implemented the below code inside onMessageReceived of FCM Message service to show notification to the user when the app is foreground.

Notification is successfully raised and shown in the tray properly. But , my requirement is , when the app is foreground the notification tray to come in front for few seconds and then it has to hide by default. How to achieve this?

NotificationCompat.Builder notificationBuilder = new 
NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);
 NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(2 , notificationBuilder.build());

Thanks

So if you want to show the notification tray programmatically, do this:

Object sbservice = getSystemService( "statusbar" );
Class<?> statusbarManager = Class.forName( "android.app.StatusBarManager" );
Method showsb;
if (Build.VERSION.SDK_INT >= 17) {
    showsb = statusbarManager.getMethod("expandNotificationsPanel");
}
else {
    showsb = statusbarManager.getMethod("expand");
}
showsb.invoke( sbservice );

You also need to add the following permission to Manifest:

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />

将通知优先级设置为Notification.PRIORITY_HIGH或Notification.PRIORITY_MAX

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM