该应用程序非常简单。我有一个常规的应用程序与所有活动等...
我有一个服务,它会在应用程序第一次启动和/或设备重启时启动。
我设置了一个BroadcastReceiver来处理通知意图,它也很基本,功能也很完美。
当Android设备上的显示开启时,通知工作正常。但是当我关闭屏幕时,不再创建通知。
我是通知新手,所以这可能是我忽略的一些简单,但我似乎无法找到它。
在我的服务中,我构造了一个工作线程,其中包含一个Timer,它每分钟触发一次,并将TimerTask作为schedule方法的一部分:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
boolean isStopping = false;
if (intent != null) // if the system has to shut us down, go down gracefully
{
String intentAction = intent.getStringExtra(AppData.BROADCAST_ACTION);
if (intentAction != null && intentAction.equals("android.intent.action.REBOOT"))
{
boolean isRebooting = intent.getBooleanExtra(AppData.SYSTEM_ACTION, false);
if (isRebooting)
{
isStopping = true;
stopSelf();
}
}
}
if (!isStopping && !_workerThread.isRunning())
{
_workerThread.run();
}
return Service.START_STICKY;
}
_timer.schedule(_service.getWorker(), startDelay, executionDelay);
NotificationTask(TimerTask)最终每分钟从Timer中被触发。然后检查一些逻辑以查看是否需要创建任何通知,如果是,它会创建并发送通知,如下所示:
Notification notification = new NotificationCompat.Builder(_context)
.setContentTitle(getString(R.string.notification))
.setContentText(message)
.setLargeIcon(decodeResource(_context.getResources(), iconId))
.setSmallIcon(R.drawable.logo_mark)
.setTicker(ticker)
.setAutoCancel(true)
.setSound(getRingtone(ringtoneUrl))
.setVibrate(VIBRATION_PATTERN)
.setLights(0xfff89829, 300, 5000)
.setContentIntent(intent)
.build();
_notificationManager.notify(notificationId, notification);
通知类型的通知是唯一的,我不在乎用户是否在用下一个覆盖之前对其进行了响应。我的问题是如果屏幕关闭,我不会收到通知。
请帮忙。
我想添加代码以防万一有人像我一样陷入困境。
让我们从AndroidManifest开始吧。包括2个BroadcastReceivers和您的服务
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<service android:name=".services.NotificationService"/>
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".receivers.NotificationReceiver" />
启动接收器只是在您的设备重新启动时安排警报。你把它设置为像这样打电话给你的其他接收器:
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, NotificationReceiver.class), 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance(), 60 * 1000, pendingIntent);
主接收器将启动您的服务 - 您的服务应该扩展IntentService而不是常规服务。这是主接收器:
context.startService(new Intent(context, NotificationService.class));
服务
public class NotificationService extends IntentService
{
public NotificationService()
{
super("Notification Service");
}
@Override
public void onHandleIntent(Intent intent)
{
// this is where I created my notifications
}
}
最后一个小技巧是在应用程序中再次创建AlarmManager。这将替换可能已在引导接收器中创建的AlarmManager。注意PendingIntent.FLAG_CANCEL_CURRENT。这样做是因为当用户启动您的应用程序时,如果AlarmManager尚未在设备启动时注册,则会注册它。
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, Utils.getNow(), 60 * 1000, pendingIntent);
希望这有帮助。