【问题标题】:Android - How to bring notification tray for few seconds in frontAndroid - 如何将通知托盘放在前面几秒钟
【发布时间】:2018-03-20 10:14:02
【问题描述】:

我在 FCM 消息服务的 onMessageReceived 中实现了以下代码,以便在应用处于前台时向用户显示通知。

通知已成功提出并正确显示在托盘中。 但是,我的要求是,当应用程序处于前台时,通知托盘会在前面几秒钟,然后它必须默认隐藏。如何做到这一点?

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

谢谢

【问题讨论】:

  • 这个答案对你有帮助吗!?

标签: android firebase-cloud-messaging android-notifications android-notification-bar


【解决方案1】:

因此,如果您想以编程方式显示通知托盘,请执行以下操作:

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

您还需要在 Manifest 中添加以下权限:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 2017-04-06
      • 2014-05-18
      • 2022-10-21
      • 1970-01-01
      相关资源
      最近更新 更多