2013-05-09 2 views
1

을 보여 가져올 수 없습니다 ... 나는 this 가이드를 따라하고, 그것이 doInBackground() 방법에서라고 AsyncTask에 다음 코드를 생성 :알림이 상태 표시 줄 알림이 동작하지 않습니다

private void newNotification(int count) { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext()); 
    builder.setContentTitle("New Notification"); 
    builder.setContentText("Testing notification"); 
    builder.setNumber(count); 

    // Creates an explicit intent for an Activity in your app 
    Intent intent = new Intent(handler.getContext(), MainActivity.class); 

    // 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(handler.getContext()); 

    // 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(intent); 
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(pendingIntent); 
    NotificationManager notificationManager = (NotificationManager) handler.getContext().getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, builder.build()); 
} 

handler 내가 매개 변수를 1로 전화를 몇 가지 이유를 들어 MainActivity

this.getContext()을 반환 getContext()를 구현하는 인터페이스를 사용하여, MainActivity에 대한 참조입니다 아무 일도 발생하지 않습니다.

@Override 
protected String[] doInBackground(String... empty) { 
    newNotification(1); 
    return null; 
} 

디버그는 코드의 모든 행은 실행 중이지만 알림은 표시되지 않음을 보여줍니다. 특수 권한에 대한 언급이 없거나 UI 스레드에서 실행해야합니다. 백그라운드 서비스는 메시지가 도착할 때처럼 알려주 려하기 때문입니다.

내가 뭘 잘못했는지 아이디어가 있습니까?

+0

보면,이 사람은이 질문에 자신 http://stackoverflow.com/questions/16314964/notification-sony-devices-으로, 자신의 특정 장치에 메신저 빌더를 사용하여 있다고 말했다 do not-show-any-notification/16315199 # 16315199 – JRowan

+1

hmmm .. 음 Xperia가 아닌 Nexus 4를 사용하고 있습니다. 재고가 충분해야 안드로이드가 잘 작동 할 것으로 기대합니다 ... 작성자는 공식 google의 일부입니다. 결국 SDK. 다른 사람이 뭔가를 알고 있는지 보자. 필자는 자신의 알림을 만드는 것보다 빌더를 사용하는 것을 선호합니다. – tbkn23

+0

AsyncTask의 onPostExecute() 또는 onProgressUpdate()를 시도하십시오. 알림은 UIthread로 처리해야합니다. – JRowan

답변

0

1) UI 스레드에서 게시를 시도 했습니까?

2) 서비스는 기본적으로 별도의 스레드에 명시된 경우가 아니면 UI 스레드에서 실행되기 때문입니다.

NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext()); 
builder.setContentTitle("New Notification"); 
builder.setContentText("Testing notification"); builder.setNumber(count); 
builder.setSmallIcon(R.drawable.ic_launcher); 

Intent notificationIntent = new Intent(this, YourClass.class); 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(getApplicationContext(), "hello", "hello", contentIntent); 

mNotificationManager.notify(1, builder.build()); 
+0

UI 스레드를 시도했지만 아무 것도 변경하지 않았습니다. 어떠한 문서에서도 UI 스레드에서만 알림을 사용할 수 있다는 언급은 없습니다. – tbkn23

+0

코드가 작동하지 않습니다. UI 또는 백그라운드 스레드가 아닙니다. – tbkn23

+0

다음과 같이 변경하십시오. int icon = R.drawable.XXXX; (여기서 xxxx는 드로어 블의 아이콘입니다.) 알림 알림 = 새 알림 (아이콘, "hello", when); 일부 정수로 HELLO_ID를 설정하면 2가됩니다. – vkinra

0

내가 파일을 다운로드 나의 작업에서 알림을 사용, 그 상태를 확인하고 마지막으로 파일을 열 수있는 알림을 제공합니다

3) 왜 같이, 먼저 간단한 예제를 시도하지 말라 pdf 뷰어. 그것은 당신에게 도움이 될 수 있습니다.

public class MyTask extends AsyncTask<Void, Void, String> 
{ 
int     NOTIFICATION_ID  = (int) System.currentTimeMillis(); 
NotificationManager notificationManager; 
Notification   notification; 

public MyTask() { 

} 

protected void onPreExecute() 
{ 
} 

protected String doInBackground(Void... params) 
{ 
    [...] 
     // Set initial notification 
     notification = new Notification(R.drawable.ic_action_export_statusbar, "Download of " + downloadSubject + " started...", 
       System.currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE; 
     notification.contentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 0, new Intent(), 0); 
     notification.contentIntent = pendingIntent; 
     notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_action_export_statusbar); 
     notification.contentView.setTextViewText(R.id.status_text, "Download of " + downloadSubjectShort + " in progress..."); 
     notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false); 
     notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(NOTIFICATION_ID, notification);   
    [...] 
} 

protected void onCancelled() 
{ 
    // remove the notification 
    notificationManager.cancel(NOTIFICATION_ID); 
} 

protected void onPostExecute(String pdfPath) 
{ 
    // remove the notification 
    notificationManager.cancel(NOTIFICATION_ID); 
    if (pdfPath != null) 
    { 
     // show final notification to open pdf 
     notification = new Notification(R.drawable.ic_action_export_finished_statusbar, "Download of " + downloadSubject 
       + " finished...", System.currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL; 
     Intent notifyIntent = new Intent(); 
     notifyIntent.setAction(android.content.Intent.ACTION_VIEW); 
     File file = new File(pdfPath); 
     notifyIntent.setDataAndType(Uri.fromFile(file), "application/pdf"); 
     notification.setLatestEventInfo(appContext, "Download finished", "open downloaded file", 
       PendingIntent.getActivity(appContext, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK)); 
     notificationManager.notify(NOTIFICATION_ID + 1, notification); 
    } else 
    { 
     // show final notification to inform about failure 
     notification = new Notification(R.drawable.ic_action_export_failed_statusbar, "Error in download", 
       System.currentTimeMillis()); 
     notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL; 
     notification.setLatestEventInfo(appContext, "Download failed", "Error", 
       PendingIntent.getActivity(appContext, 0, new Intent(), android.content.Intent.FLAG_ACTIVITY_NEW_TASK)); 
     notificationManager.notify(NOTIFICATION_ID + 1, notification); 
    } 
} 

} 이것

관련 문제