2012-10-19 33 views
39

Tôi vừa bắt đầu làm việc với thông báo và giờ tôi đang cố gắng xóa thông báo và khởi chạy ứng dụng sau khi thông báo đã được nhấn vào trong trung tâm thông báo.Xóa thông báo sau khi nhấp vào

tôi đã cố gắng để làm việc với đoạn mã sau:

import android.app.NotificationManager; 

public class ExpandNotification { 
    private int NOTIFICATION = 546; 
    private NotificationManager mNM; 

    public void onCreate() { 
     mNM.cancel(NOTIFICATION); 
     setContentView(R.layout.activity_on); 
     //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show(); 
    } 

Tôi nghĩ rằng mã này thực thi các lớp khác khi khai thác?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0); 

Tuy nhiên thông báo không biến mất, cũng như không khởi chạy ứng dụng. Nhưng tôi có thể swipe nó sang trái hoặc phải để xoá bỏ nó nhưng đó không phải là những gì tôi muốn ..

Trả lời

71

Sử dụng cờ Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); 
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); 

// Cancel the notification after its selected 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 

và để khởi động ứng dụng:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

// Create a new intent which will be fired if you click on the notification 
Intent intent = new Intent(context, App.class); 

// Attach the intent to a pending intent 
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

Cảm ơn, điều này đã thực hiện công việc :) – user1756912

+0

Rất vui khi được trợ giúp!:) – input

+2

Điều gì về tình huống khi bạn không chỉ định ContentIntent (chỉ muốn thông báo được xóa khi khai thác)? Trong trường hợp này auto_cancel flag dường như không giúp được gì ... –

90

Để có được hiệu ứng tương tự bằng cách sử dụng Notification.Builder hoặc NotificationCompat.Builder, hãy gọi setAutoCancel(true) trên phiên bản Trình tạo.

+0

Cảm ơn .. nó hoạt động .. –

6

Câu trả lời này là quá nhiều muộn nhưng đặc biệt tôi viết sau đây giải pháp vì constructor thông báo trở thành bị phản đối nên sử dụng thông báo sử dụng xây dựng, như sau:

**.setAutoCancel(true)** is used to remove notification on click 

và toàn bộ thông báo là như follwoing:

private void makeNotification(String title,String msg){ 

    Intent resultIntent = new Intent(this, MasterActivity.class); 

    PendingIntent resultPendingIntent = 
      PendingIntent.getActivity(
        this, 
        0, 
        resultIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
        .setContentIntent(resultPendingIntent) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setContentTitle(title) 
        .setAutoCancel(true) 
        .setContentText(msg); 

    int mNotificationId = 001; 
    NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

} 

Gọi phương thức này với tiêu đề và tin nhắn bạn sẽ nhận được thông báo hoàn hảo.

2

Tốt nhất & cách đơn giản được đặt builder.setAutoCancel(true) nó sẽ hủy thông báo của bạn sau khi nhấp vào thông báo. Tôi hy vọng mã này sẽ giúp bạn.

Builder để thông báo

NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
builder.setSmallIcon(android.R.drawable.btn_star); 
builder.setContentTitle("This is title of notification"); 
builder.setContentText("This is a notification Text"); 
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

Đối mở một hoạt động trên cách nhấn vào thông báo

Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT); 
builder.setContentIntent(pendingIntent); 
builder.setAutoCancel(true); 

Hiện trình tạo trong thông báo

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
manager.notify(114, builder.build()); 

mã hoàn chỉnh cho chương trình thông báo với biểu tượng, hình ảnh, tiêu đề, mô tả, tự động hủy và khi nhấp mở một hoạt động

public void ShowIntentNotification(View v) 
    { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
     builder.setSmallIcon(android.R.drawable.btn_star); 
     builder.setContentTitle("This is title of notification"); 
     builder.setContentText("This is a notification Text"); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

     Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     builder.setContentIntent(pendingIntent); 
     builder.setAutoCancel(true); 

     NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     manager.notify(114, builder.build()); 

    } 
0

Đối với nó tôi là

.setPriority(Notification.PRIORITY_HIGH); 

đã gây ra thông báo không rõ ràng sau khi nhấp chuột ... chắc chắn rằng bạn sử dụng:

.setPriority(Notification.PRIORITY_DEFAULT); 

.setAutoCancel(true) nên làm việc.

Các vấn đề liên quan