如何在构建器通知中设置持续标志?

时间:2017-01-07 21:30:37

标签: android

如何在使用Notification.Builder构建器创建的通知中添加标记?向Notification noti = new NotificationCompat.Builder(this)通知声明添加标记是直截了当的,但不是我上面引用的那个。这是我到目前为止所尝试的内容:

Notification.Builder builder = new Notification.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Download [ROOT]")
    //.setContentText("This is a test notification")
    .setAutoCancel(false).setOngoing(true);
Intent notificationIntent = new Intent(this, NotificationAction.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(111, builder.build());

1 个答案:

答案 0 :(得分:0)

for setOngoing()到Notification.Builder试试这个:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentIntent(generatePendingIntent("Notification Type"))
.setAutoCancel(true)
.setOngoing(true/false); // this is where you have to set

您也可以使用Notification.Builder,但使用NotificationCompat.Builder,在Android 4.1及更高版本中,您可以设置action buttons,具体取决于展开的通知。

并处理点击通知方法时会发生什么:

private PendingIntent generatePendingIntent(String type) {
    PendingIntent pendingIntent;
    Intent notificationIntent;
    if (type.equals("link")) {
        notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
    } else {
        notificationIntent = new Intent(context, DesiredActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pendingIntent =
                PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, 0);
    }
    return pendingIntent;
}