我正在嘗試使用推送通知提醒用戶。每天早上提醒用戶的Android應用程序
我一直在使用AlarmManager,BroadcastReceiver和IntentService。
在下面的代碼中,我以60秒的間隔測試了我的AlarmManager。
問題: 一切工作正常,直到我關閉我的應用程序,警報不再發射。
任何想法下一步去哪裏?
清單:
<service
android:name=".IntentMentor"
android:exported="false" >
</service>
<receiver android:name=".AlertReceiver"
android:process=":remote">
</receiver>
MainActivity:
Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
// 1s is only for testing
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);
接收機:
public class AlertReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "MyReceiver on receive");
Intent i = new Intent(context, IntentMentor.class);
context.startService(i);
}
}
IntentService:
public class IntentMentor extends IntentService {
NotificationManager notificationManager;
int notifID = 33;
private static final String TAG = "MonitorService";
public IntentMentor() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("TAG", "Service method was fired.");
pushNotification();
}
public void pushNotification(){
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("test");
notificBuilder.setContentText("this is text");
notificBuilder.setTicker("this is Ticker?");
notificBuilder.setSmallIcon(R.drawable.ic_info_black_24dp);
notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
Intent intent = new Intent(this, Card.class);
TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
tStackBuilder.addParentStack(Card.class);
tStackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
notificationManager.notify(notifID, notificBuilder.build());
}
}
[嘗試,這可能是這個幫助你](http://stackoverflow.com/questions/35121191/i-want-show-notification-at-800-am-everyday/35127736#35127736) –
我試圖解決方案在你的鏈接後面。謝謝..但我仍然有同樣的問題。當應用程序未打開時,警報不會觸發。 – Wiltson
請確保您授予WAKE_LOCK的權限 –