Android 通知时间 [英] Android Notification at time
问题描述
我现在正在寻找如何做到这一点:
I am looking for hours now how to do this exactly:
我希望每天(周末除外)一次发送通知(假设 18:00(= 下午 6 点)),除非应用程序已经打开.当您收到邮件时,它基本上就像 gmail 应用程序.当用户点击通知时,它应该会消失并被带到 MainActivity.
I want that every day (except weekends) a notification is sent at a time (let's say 18:00 (= 6pm)) except for when the app is open already. It's basically like the gmail app when you receive a mail. When the user clicks the notification it should disappear and should be brought to the MainActivity.
我使用 AlarmManager 尝试了很多方法,但都没有导致显示通知.
I have tried numerous things with the AlarmManager, but none have resulted in a notification showing up.
我试过的代码,我觉得非常接近正确,如下:在我的主活动中:
The code I tried, which I feel is pretty close to being correct, is following: In my MainActivity:
AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
我的通知服务:
public class NotificationService extends IntentService {
public NotificationService() {
super("NotificationService");
}
@Override
@SuppressWarnings("deprecation")
protected void onHandleIntent(Intent intent) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "reminder", System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
nm.notify(1, notification);
}
}
注意我使用了不推荐使用的东西,因为这甚至是在不使用 AlarmManager 时出现通知的唯一方式.如果可能,请使用不含已弃用内容但使用最新内容的解决方案 :P.
Notice I use deprecated stuff because this was even the only way a notification ever showed up while not working with the AlarmManager. If possible, please react with a solution that does not have the deprecated stuff in it, but with up to date stuff :P.
提前致谢!!!
最诚挚的问候
推荐答案
我终于找到了解决方案:
Finally I was able to find the solution:
private void handleNotification() {
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
}
这是我的自定义BroadcastReceiver
:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
int dayOfWeek = now.get(Calendar.DATE);
if(dayOfWeek != 1 && dayOfWeek != 7) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getResources().getString(R.string.message_box_title))
.setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date));
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
}
这在应用程序标签的清单中:
And this in the manifest in the application tag:
<receiver
android:name="be.menis.timesheet.service.AlarmReceiver"
android:process=":remote" />
这篇关于Android 通知时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!