来自Android广播接收器的通知不工作
我正在尝试在我的应用程序进行每日检查时通知用户。代码运行正常,但通知从未出现在状态栏中。有人知道为什么吗来自Android广播接收器的通知不工作,android,notifications,broadcastreceiver,Android,Notifications,Broadcastreceiver,我正在尝试在我的应用程序进行每日检查时通知用户。代码运行正常,但通知从未出现在状态栏中。有人知道为什么吗 public class OnNotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { WakeReminderIntentService.acquireStaticLock(context); Int
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);
}
}
编辑:我试着将它复制到我的主线程(通知方法)中,效果很好。所以一定是广播接收器的问题
编辑2:传递给它的上下文是MainActivity。此(我的主要活动)请尝试通知顶部的这一行,然后将此上下文用于方法的其余部分:
Context context = ctx.getApplicationContext();
我不确定这能完成什么…但我无论如何都试过了,但没有成功。