Android點擊通知欄 ,移除通知

通過點擊系統通知欄移除 通知只需要一個方法
Notification notification = new Notification();
notification.setAutoCancel(true);

下面是整個系統彈窗的方法已做了6.0設配
/**
* 頭部系統消息提示
* 狀態欄提示消息
*/
private void setNotificationDemoForAndroidO(int progress, String content, Context context) {
//ID
String id = “testNotification”;
//名稱
String name = “notification”;

    NotificationManager notificationManager = (NotificationManager) context.getSystemService
            (NOTIFICATION_SERVICE);
    Notification.Builder mBuilder = new Notification.Builder(context);
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_notification);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager
                .IMPORTANCE_DEFAULT);//FLAG_AUTO_CANCEL
        mBuilder.setChannelId(id);
        notificationManager.createNotificationChannel(channel);

        mBuilder.setSmallIcon(R.drawable.logo);

// if (CGlobalData.ifOpenTest != true) {
// Intent intent = new Intent(context, SystemMessagesActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// mBuilder.setContentIntent(pendingIntent);

        Intent intent= new Intent();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        mBuilder.setAutoCancel(true);//點擊通知欄移除通知
        mBuilder.setContentIntent(pendingIntent);

// EventBus.getDefault().post(new SystemMessageClose(“finish”));//關閉
// }
mBuilder.setContent(remoteViews);
if (progress == 1) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
remoteViews.setImageViewResource(R.id.iv_notification_image, R.drawable.logo);
remoteViews.setTextViewText(R.id.tv_notification_title, “”);
remoteViews.setTextViewText(R.id.tv_notification_content, content);
remoteViews.setProgressBar(R.id.pBar, 10, progress, false);
remoteViews.setTextViewText(R.id.proNum, progress + “/10”);
} else {
mBuilder.setSmallIcon(R.drawable.logo);
// if (CGlobalData.ifOpenTest != true) {
// EventBus.getDefault().post(new SystemMessageClose(“finish”));//關閉
// Intent intent = new Intent(context, SystemMessagesActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// mBuilder.setContentIntent(pendingIntent);

        Intent intent= new Intent();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        mBuilder.setAutoCancel(true);//點擊通知欄移除通知
        mBuilder.setContentIntent(pendingIntent);

            mBuilder.setAutoCancel(true);//點擊通知欄移除通知

// }
// SystemMessagesCallBack.doCallBackMethod();

// EventBus.getDefault().post(new BaseEvent(“SystemMessages”));
mBuilder.setContent(remoteViews);
if (progress == 1) {
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
}
remoteViews.setImageViewResource(R.id.iv_notification_image, R.drawable.logo);
remoteViews.setTextViewText(R.id.tv_notification_title, “”);
remoteViews.setTextViewText(R.id.tv_notification_content, content);
remoteViews.setProgressBar(R.id.pBar, 10, progress, false);
remoteViews.setTextViewText(R.id.proNum, progress + “/10”);
}
// notificationManager.notify(10, mBuilder.build());
notificationManager.notify(1, mBuilder.build());

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章