Android通知时间

Val*_*ire 10 notifications android alarmmanager android-notifications

我现在正在寻找几个小时如何做到这一点:

我希望每天(除了周末)一次发送通知(比如18:00(= 6pm)),除非应用程序已经打开.当你收到邮件时,它基本上就像是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);
Run Code Online (Sandbox Code Playgroud)

我的通知服务:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意我使用弃用的东西,因为这甚至是不使用AlarmManager时通知出现的唯一方式.如果可能的话,请与一个没有弃用的东西的解决方案做出反应,但要使用最新的东西:P.

很多很多人提前感谢!!!

最亲切的问候

Val*_*ire 10

最后我找到了解决方案:

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);
}
Run Code Online (Sandbox Code Playgroud)

这是我的习惯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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这在应用程序标记的清单中:

<receiver
        android:name="be.menis.timesheet.service.AlarmReceiver"
        android:process=":remote" />
Run Code Online (Sandbox Code Playgroud)