2012-07-15 54 views
2

我试图在我的应用程序每天检查时通知用户。代码运行良好,但通知从未出现在状态栏中。任何人都能看到为什么来自Android广播接收器的通知不正常

public class OnNotificationReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    WakeReminderIntentService.acquireStaticLock(context); 
    Intent i = new Intent(context, NotificationService.class); 
    context.startService(i); 

    Bundle extra = intent.getExtras(); 
    int icon = extra.getInt("icon"); 
    notification(context, icon); 

    Log.d("DailyCheck", "Checking for sales"); 
} 
private void notification(Context ctx, int icon) { 
     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns); 


     CharSequence tickerText = "ticker"; 
     long when = System.currentTimeMillis() + 1000; 


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


     CharSequence contentTitle = "Title Here"; 
     CharSequence contentText = "Text here"; 
     Intent notificationIntent = new Intent(ctx, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0); 

     notification.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent); 
     notification.flags |= Notification.DEFAULT_SOUND; 

     mNotificationManager.notify(9999, notification); 
    } 

}

编辑:我试着将它复制到我的主线程(通知方法),它工作得很好。所以它必须是BroadcastReceiver的东西?

编辑2:背景信息被传递到它是MainActivity.this(我的主要活性)

回答

1

在通知顶端试这条线,然后使用该环境的方法的其余部分:

Context context = ctx.getApplicationContext(); 
+0

不知道这会完成什么......但我试过了,但它没有奏效。 – easycheese 2012-07-15 22:01:10