繁体   English   中英

BigTextStyle通知

[英]BigTextStyle notification

我必须在我的应用程序中添加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();

}

创建一种方法来取消已编写代码的类中的通知通知,

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

并且,如果从通知中打开了EmptyActivityActionTakenActivity ,请从onCreate()方法中调用此方法以取消通知。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM