简体   繁体   English

Android-如何将通知托盘提前几秒钟

[英]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. 我在FCM消息服务的onMessageReceived内部实现了以下代码,以在应用程序为前台时向用户显示通知。

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: 您还需要向Manifest添加以下权限:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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