我正在尝试在我的应用进行每日检查时通知用户。代码运行正常但通知从未出现在状态栏中。谁能明白为什么?
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(我的主要活动)
答案 0 :(得分:1)
在通知顶部尝试此行,然后将此上下文用于方法的其余部分:
Context context = ctx.getApplicationContext();