android - 抬头通知在几秒钟后被解雇

标签 android android-notifications heads-up-notifications

我正在尝试通过下面的代码发出提醒通知,并希望它一直存在,直到用户决定关闭它。但它会在几秒钟后(大约 10 秒)自动消失。有什么方法可以让它持久化并留给用户关闭它。

NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setContentText("Hello !!!")
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher,
"View Call", null)
.addAction(R.drawable.ic_launcher,
"Call Back", null);

// Gets an instance of the NotificationManager service
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());

最佳答案

这段代码对我有用。

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent piDismiss = PendingIntent.getActivity(this, 0, intent, 0);

        //build notification
        NotificationCompat.Builder builder =
        new NotificationCompat.Builder(this)
        .setCategory(Notification.CATEGORY_MESSAGE)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Ping Notification")
        .setContentText("You have a new notification.")
        .setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
        .setPriority(NotificationCompat.PRIORITY_DEFAULT) //must give priority to High, Max which will considered as heads-up notification
        .addAction(R.drawable.dismiss,
        "View Call", piDismiss)
        .addAction(R.drawable.ic_ok,
        "ok", null)
        .setFullScreenIntent(piDismiss, true);

关于android - 抬头通知在几秒钟后被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39144144/

相关文章:

android - Cordova 无法使用 cordova-plugin-local-notifications 构建

android - NotificationCompat.Builder 中的 NoClassDefFound

android - 提醒通知未按预期工作

android - 关闭 Heads Up 通知并创建一个普通通知

javascript - Phonegap 不显示警报

android - 我在 android 中创建 amazon cognito 后出现错误

java - 所以我偶然发现了一些东西......文件扫描媒体扫描仪连接

java - 从 javascript 函数访问 Java String 对象

android - 使用无法在 Android 上运行的 Facebook ID 向特定用户发送 Parse 推送通知

android - 不启动新 Activity 的通知操作?