2013-05-31 34 views
6

Tôi muốn làm thông báo nghỉ Android của tôi ngay cả khi người dùng nhấp chuột hay bấm rõ ràng tất cả ...Làm thế nào để làm cho thông báo uncancellable/unremovable

Ngay bây giờ nó vẫn thỉnh thoảng, và bị loại bỏ đôi khi, và tôi không chắc chắn điều gì gây ra nó.

Dưới đây là mã của tôi cho thông báo:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public static void createNotification() 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
          .setContentTitle("Wolftech Field Agent") 
          .setContentText("Getting Status") 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setOngoing(true) 
          .setAutoCancel(false); 

    Intent intent = new Intent(context, FieldAgent.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(FieldAgent.class); 
    stackBuilder.addNextIntent(intent); 

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(pendingIntent); 

    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

public static void updateNotificationText(String inString) 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
              .setContentText(inString) 
              .setContentTitle("Wolftech Field Agent") 
              .setSmallIcon(R.drawable.ic_launcher) 
              .setOngoing(true) 
              .setAutoCancel(false); 

    Intent intent = new Intent(context, FieldAgent.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(FieldAgent.class); 
    stackBuilder.addNextIntent(intent); 

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(pendingIntent); 

    notificationManager.notify(NOTIFICATION_ID, builder.build()); 
} 

public static void cancelNotification() 
{ 
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.cancel(NOTIFICATION_ID); 
} 

Trả lời

1

Okey, tôi đã nhận ra mã tôi đăng thực sự tốt.

Điều đang xảy ra là khi tôi nhấp vào thông báo, nó sẽ gọi onCreate() một lần nữa, và sau đó sau một khoảng thời gian ngẫu nhiên nó gọi onDestroy().

Trong onDestroy() Tôi đã có phương thức cancelNotification() của tôi, vì vậy nếu onDestroy() được gọi sau onCreate(), nó đã xóa thông báo của tôi.

Điều này mang lại cho tôi một câu hỏi mới: Tại sao nó phá hủy và tái tạo hoạt động của tôi sau khi tôi đã làm theo mọi câu trả lời tôi có thể tìm thấy ở đây về cách ngăn chặn điều đó xảy ra?

Bạn có thể tìm thấy câu hỏi đó đây How to make notification resume and not recreate activity? nếu bạn muốn giúp tôi giải quyết nó ...

1

bạn cố gắng

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
notificationManager.cancel(pendingIntent); 
+0

Tôi nghĩ bạn đã hiểu sai câu hỏi (hoặc tôi hiểu nhầm phản hồi của bạn ...) Tôi không muốn hủy thông báo (tốt, khi tôi quyết định), tôi muốn thông báo ở lại mọi bộ hẹn giờ người dùng nhấp vào nó hoặc nhấp vào "xóa tất cả". – CeeRo

4

Tôi tin rằng bạn đang tìm kiếm cờ này:

Notification.FLAG_NO_CLEAR 

Thêm cờ đó để thông báo của bạn .

+0

Dường như tôi ở lại khi tôi nhấp vào xóa tất cả, nhưng nó vẫn biến mất đôi khi tôi nhấp vào nó. Weird – CeeRo

+0

Ngay bây giờ tôi đang nghiêng về việc tái tạo/cập nhật thông báo trên onResume() để nó được làm lại sau mỗi lần nhấp, có vẻ như không phải là một cách để đảm bảo rằng nó thực sự ở lại khác ... – CeeRo

+0

Tôi đã tìm ra những gì gây ra nó, kiểm tra câu trả lời của tôi – CeeRo

8

Tôi nghĩ rằng bạn đang tìm kiếm thông báo đang diễn ra, trong trường hợp đó nếu bạn đang sử dụng NotificationCompat.Builder, bạn có thể sử dụng:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this); 
mNotifyBuilder.setOngoing(true); 
+0

Đây có phải là dịch vụ tiền cảnh không? –

-1

Tôi cũng gặp sự cố này. Giải pháp của tôi là gắn thêm vào chức năng tạo ra mục đích

 private void addNotification(String headLine, String info, Context context) { 
     mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.icon) //R.drawable.icon 
         .setContentTitle(headLine) 
         .setContentText(info) 
         .setOngoing(true) 
         .setAutoCancel(false); 
// Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(context, MainActivity.class); 
     resultIntent.putExtra("FROM_NOTIF", true); 
// The stack builder object will contain an artificial back stack for the 
// started Activity. 
// This ensures that navigating backward from the Activity leads out of 
// your application to the Home screen. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
// Adds the back stack for the Intent (but not the Intent itself) 
     //  stackBuilder.addParentStack(MainActivity.class); 
     //Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 

     mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
// mId allows you to update the notification later on. 
     //  mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 
     mNotificationManager.notify(mNotification_id, mBuilder.build()); 
    } 

và In MainActivity:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    if (intent.getBooleanExtra("FROM_NOTIF", false)) { 
     addNotification(...) 
      ... call the function that adds the notification again ..} 

Vì vậy Cái này là thông báo sẽ tắt sau khi báo chí sử dụng trên đó, nhưng hơn bạn có thể được thông báo về hoạt động đã bắt đầu sử dụng onNewIntent(), kiểm tra ý định trên đó và nếu bạn phát hiện ra rằng nó đến từ thông báo, hãy đặt lại.

+0

Trong khi mã này có thể trả lời câu hỏi, cung cấp ngữ cảnh bổ sung về cách thức và/hoặc lý do giải thích vấn đề này sẽ cải thiện giá trị lâu dài của câu trả lời. Vui lòng đọc [stack-flow] này [how-to-answer] (http: // stackoverflow).com/help/how-to-answer) để cung cấp câu trả lời có chất lượng. – thewaywewere

+0

Đã chỉnh sửa câu trả lời của tôi –

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