【发布时间】:2019-10-07 13:40:27
【问题描述】:
Android 提醒通知会出现几秒钟,然后从主屏幕消失并停留在状态栏和通知抽屉中。有没有办法在用户交互之前在主屏幕上显示弹出通知?
【问题讨论】:
标签: java android notifications
Android 提醒通知会出现几秒钟,然后从主屏幕消失并停留在状态栏和通知抽屉中。有没有办法在用户交互之前在主屏幕上显示弹出通知?
【问题讨论】:
标签: java android notifications
您需要为收到通知时将打开的活动创建新活动。 收到通知后,您需要将数据中的有效负载设置为密钥对值,否则它将由 Android 操作系统处理。
这会有所帮助-Android Activity as a dialog
How to handle notification when app in background in Firebase
【讨论】:
如果您不介意将通知变成快餐栏,您可以展示一个在用户与之交互之前不会消失的通知:
public void showSnakbarIndef(final View rootView, String message, String actionMessage) {
Snackbar.make(rootView, message, Snackbar.LENGTH_INDEFINITE)
.setAction(actionMessage, new View.OnClickListener() {
@Override
public void onClick(View v) {
//exit or other action according to notification
}
})
.show();
}
【讨论】:
在Notification.Builder 中使用setOngoing(true) 和setAutoCancel(true),如下所示:
notification = new Notification.Builder(getApplicationContext())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setAutoCancel(true)
.setContentText(contentText)
.setContentIntent(contentIntent)
【讨论】:
NotificationCompat.Builder 中的 setFullScreenIntent
【讨论】: