当我的应用程序进行每日检查时,我试图通知用户。代码运行良好,但通知永远不会出现在状态栏中。谁能明白为什么?
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 (我的主要活动)