2012-01-27 42 views
10

Tôi đã cố gắng nhận được thông báo về việc tải lên thành công từ ASyncTask để hoạt động cả ngày. Tôi không nhận được bất kỳ lỗi nào từ mã hiện tại của mình nhưng tôi không thể nhận được thông báo hiển thị trên thanh thông báo (hoặc bất kỳ nơi nào khác). Tôi không nhận được tin nhắn nào trong LogCat và không có thông báo nào xuất hiện trên thanh Notification. Đây là mã của tôi:Thông báo Android không hoạt động

Notification mNotification = new Notification(icon, tickerText, when); 

CharSequence contentTitle = "upload completed."; 
CharSequence contentText = "upload completed."; 

Intent notificationIntent = new Intent(context, CastrActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_NO_CREATE); 
mNotification.contentIntent = contentIntent; 
mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
mNotificationManager.notify(NOTIFICATION_ID, mNotification); 

Điều này được gọi từ phương thức onPostExecute() của ASyncTask. Tôi hơi bối rối về phần PendingIntent, thành thật mà nói. Bất kỳ làm rõ những gì tôi nghi ngờ là mã không chính xác sẽ có rất nhiều đánh giá cao.

Trả lời

4

Tôi đã tạo ra lớp hiển thị thông báo:

public class NotificationData { 

    public static NotificationManager mNotificationManager; 
    public static int SIMPLE_NOTFICATION_ID; 
    private Context _context; 

    public NotificationData(Context context) { 
     _context = context; 
    } 

    public void clearNotification() { 
     mNotificationManager.cancel(SIMPLE_NOTFICATION_ID); 
    } 

    public void SetNotification(int drawable, String msg, String action_string, Class cls) { 
     mNotificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE); 
     final Notification notifyDetails = new Notification(drawable, "Post Timer", System.currentTimeMillis()); 
     long[] vibrate = { 100, 100, 200, 300 }; 
     notifyDetails.vibrate = vibrate; 
     notifyDetails.ledARGB = 0xff00ff00; 
     notifyDetails.ledOnMS = 300; 
     notifyDetails.ledOffMS = 1000; 
    // notifyDetails.number=4; 
     notifyDetails.defaults =Notification.DEFAULT_ALL; 
     Context context = _context; 
     CharSequence contentTitle = msg; 
     CharSequence contentText = action_string;  
     Intent notifyIntent = new Intent(context, cls); 
    // Bundle bundle = new Bundle(); 
    // bundle.putBoolean(AppConfig.IS_NOTIFICATION, true); 
     notifyIntent.putExtras(bundle); 
     PendingIntent intent = PendingIntent.getActivity(_context, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK); 
     notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); 
     mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);   
    } 
} 

Làm thế nào để sử dụng lớp này:

NotificationData notification; //create object 
notification = new NotificationData(this); 
notification.SetNotification(R.drawable.notification, "Notification Title", "Click to open", YourClassName.class); 

Thêm phép android.permission.VIBRATE

+0

Tôi xin lỗi nhưng AppConfig là gì? Có thư viện nào tôi cần đưa vào sử dụng không? Eclipse dường như không biết nó nếu có vì vậy tôi sẽ phải thêm nó vào đường dẫn xây dựng của tôi. – Carnivoris

+0

Appconfig là một lớp và IS_NOTIFICATION là một thành viên tĩnh, bạn có thể xóa dòng này Bundle bundle = new Bundle(); bundle.putBoolean (AppConfig.IS_NOTIFICATION, true); notifyIntent.putExtras (bundle); –

+0

Thật không may, tôi vẫn không nhận được thông báo nào được gửi. Tôi đang gọi nó từ phương thức onPostExecute() của lớp ASyncTask. Tôi xác nhận rằng ASyncTask hoàn tất bằng một tin nhắn trong LogCat, nhưng tôi không nhận được thông báo nào được gửi đến thanh thông báo. – Carnivoris

2

Hãy thử điều này:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

int icon = R.drawable.icon;  // icon from resources 
CharSequence tickerText = "Any thing";    // ticker-text 
long when = System.currentTimeMillis();   // notification time 
Context context21 = getApplicationContext();  // application Context 
CharSequence contentTitle = "Anything"; // expanded message title 
CharSequence contentText = (CharSequence) extras.get("message");  // expanded message text 

Intent notificationIntent = new Intent(this, MainStart.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

// the next two lines initialize the Notification, using the configurations above 
Notification notification = new Notification(icon, tickerText, when); 
notification.defaults |= Notification.DEFAULT_VIBRATE; 
notification.defaults |= Notification.DEFAULT_LIGHTS; 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.flags |= Notification.FLAG_AUTO_CANCEL; 
/* long[] vibrate = { 0, 100, 200, 300 }; 
notification.vibrate = vibrate; 
notification.ledARGB = Color.RED; 
notification.ledOffMS = 300; 
notification.ledOnMS = 300;*/ 
notification.setLatestEventInfo(context21, contentTitle, contentText, contentIntent); 
mNotificationManager.notify(Constants.NOTIFICATION_ID, notification); 
+0

Tôi đang gặp phải các sự cố tương tự với điều này như trước đây. Thông báo ý địnhIntent = new Intent (this, CastrRecorder.class); Dòng đó được đánh dấu bởi Eclipse và độ phân giải duy nhất cho nó là loại bỏ các đối số. Ngoài ra, điều này đang được gọi trong một lớp mở rộng ASyncTask và getActivity() không hoạt động. – Carnivoris

2

Một điều khác cần thử là đảm bảo tệp kê khai của bạn chứa

<permission android:name="android.permission.STATUS_BAR_SERVICE" android:protectionLevel="signature" /> 

Ngoài ra, tôi dường như bỏ qua các thông báo liên tiếp với cùng một NOTIFICATION_ID.

30

Mặc dù vấn đề của bạn được giải quyết, tôi sẽ chỉ gửi cách tôi giải quyết vấn đề của tôi là thông báo được không hiển thị, có lẽ nó có thể giúp đỡ người khác đọc các câu trả lời:

Trong tòa nhà thông báo của tôi, tôi đã bỏ lỡ biểu tượng. Ngay sau khi tôi thêm một cái gì đó như setSmallIcon(R.drawable.ic_launcher) thông báo đã được hiển thị.

+0

có cùng một vấn đề chính xác .. giải quyết, cảm ơn! – akhyar

+0

Có, đã làm việc cho tôi. Lần đầu tiên làm việc với thông báo.Cảm ơn nhiều! –

+0

Chính xác! Cảm ơn rất nhiều! – Alexandr

0

Đối với tôi điều này xảy ra và tôi không biết tại sao nhưng vấn đề là biểu tượng tôi đặt quá lớn nên nó đã cho tôi một số lỗi ngẫu nhiên.

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