Android Notification at time

前端 未结 1 659
别那么骄傲
别那么骄傲 2020-12-16 01:56

I am looking for hours now how to do this exactly:

I want that every day (except weekends) a notification is sent at a time (let\'s say 18:00 (= 6pm)) except for whe

相关标签:
1条回答
  • 2020-12-16 02:49

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

    This is my custom 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" />
    
    0 讨论(0)
提交回复
热议问题