安卓通知定时问题

12

我已经寻找了好几个小时,想要准确地实现以下内容:

我希望每天(除了周末)在某个特定时间(比如说下午6点)发送一条通知,但前提是应用程序没有打开。就像 Gmail 应用程序在您收到邮件时的情况一样。 当用户点击通知时,它应该消失,并且应该跳转回 MainActivity。

我尝试过很多使用 AlarmManager 的方法,但都没有成功显示出通知。

以下是我尝试过的代码,感觉非常接近正确:

在我的 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);

我的NotificationService:

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

非常非常感谢您的提前帮助!!!

最亲切的问候

1个回答

11

最终我能够找到解决方案:

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());
        }
    }
}

还有在应用标签中的清单文件中:

<receiver
        android:name="be.menis.timesheet.service.AlarmReceiver"
        android:process=":remote" />

这个android:name="be.menis.timesheet.service.AlarmReceiver"是什么?我想每天上午10点设置通知。 - Neo
1
@ashish 那是接收器的应用程序包名称和位置。 - David Passmore
谢谢你提供的代码,我正在测试它。但是有没有人知道,每次运行这段代码我是否会设置一个新的闹钟?如果是的话,我应该非常小心... - George
关闭应用程序后,这个能运行吗? - Kairi San
是的,以前它可以运行。然而,正如您所看到的,这是很久以前的事了,所以我不确定应用程序是否仍在后台运行或者它是否被真正关闭。我相信它确实被关闭了 :)。 - Valentin Grégoire

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接