3

当我的应用程序进行每日检查时,我试图通知用户。代码运行良好,但通知永远不会出现在状态栏中。谁能明白为什么?

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 (我的主要活动)

4

1 回答 1

0

在通知顶部尝试此行,然后将此上下文用于方法的其余部分:

Context context = ctx.getApplicationContext();
于 2012-07-15T21:32:21.293 回答