BigTextStyle通知

时间:2016-06-16 09:42:35

标签: android notifications

我必须在我的应用中添加BigTextStyle通知。通过使用属性添加操作我有3个按钮,并在每个按钮上执行不同的操作。现在的问题是,当我点击任何按钮时,通知未被清除。它仍然留在通知区域。任何人都可以帮助我......

这是我的方法......

private void fireTheNotificaiton(Context _notifyContext,
                                 String message,
                                 int notifiId,
                                 ActionMessage msg) {
    Notification noti = new Notification();

    noti = setBigTextStyleNotification(_notifyContext,
            message,
            notifiId,
            msg);

    noti.defaults |= Notification.DEFAULT_LIGHTS;
    noti.defaults |= Notification.DEFAULT_VIBRATE;
    noti.defaults |= Notification.DEFAULT_SOUND;
    noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager nm = (NotificationManager) _notifyContext.getSystemService(
            _notifyContext.NOTIFICATION_SERVICE);
    nm.notify(notifiId, noti);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private Notification setBigTextStyleNotification(Context _notifyContext,
                                                 String message,
                                                 int notifiId,
                                                 ActionMessage msg) {

    Bitmap icon = null;

    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle("Action:" + message);
    String time = DateFormat.getTimeInstance().format(new Date()).toString();
    notiStyle.setSummaryText(time);

    try {
        icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.truckfront);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Add the big text to the style.
    CharSequence bigText = msg.getMessage();
    notiStyle.bigText(bigText);

    session.fromactionnotification(true);
    // Creates an explicit intent for an ResultActivity to receive.
    Intent intent1 = null;
    {
        intent1 = new Intent(getApplicationContext(), ActionTakenActivity.class);
        intent1.putExtra("msg", msg);
    }

    intent1.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent = PendingIntent.getActivity(_notifyContext, 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent intent2 = null;
    {
        intent2 = new Intent(getApplicationContext(), EmptyActivity.class);
        intent2.putExtra("msg", msg);
        intent2.putExtra("yes", 1);
    }

    intent2.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent21 = PendingIntent.getActivity(_notifyContext, 0, intent2,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent intent3 = null;
    {
        intent3 = new Intent(getApplicationContext(), EmptyActivity.class);
        intent3.putExtra("msg", msg);
        intent3.putExtra("yes", 2);
    }

    intent3.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent intent31 = PendingIntent.getActivity(_notifyContext, 0, intent3,
            PendingIntent.FLAG_UPDATE_CURRENT);

    session.fromactionnotification(true);

    return new NotificationCompat.Builder(_notifyContext)
            .setSmallIcon(R.drawable.truckfront)
            .setAutoCancel(true)
            .setLargeIcon(icon)
            .addAction(R.drawable.ic_visibility_black_24dp, "View", intent)
            .addAction(R.drawable.ic_done_black_24dp, "Yes", intent21)
            .addAction(R.drawable.ic_clear_black_24dp, "Can't", intent31)
            .setContentTitle("Action:" + message)
            .setContentText(msg.getMessage())
            .setStyle(notiStyle).build();

}

1 个答案:

答案 0 :(得分:0)

创建一个方法来取消已编写代码的类中的notofication,以生成通知,

public static void cancelNotification(int notificationId) {
    mNotificationManager.cancel(notificationId);
}

如果您的EmptyActivityActionTakenActivity已从通知中打开,请从onCreate()方法调用此方法以取消通知。

public void onCreate(Bundle savedInstanceState) {
    NotificationHelper.cancelNotification(notificationId);
}