如何每天早上7点显示本地通知

问题描述 投票:1回答:1

一切正常,通知运行正常。但是我已经在必须显示通知时在MainActivity中设置了时间。但我认为它无法正常运行。我在晚上12点收到通知:(我做错了。请帮助。

我的通知助手代码,

public class NotificationHelper {

    private Context mContext;
    private static final String NOTIFICATION_CHANNEL_ID = "10001";


    NotificationHelper(Context context) {
        mContext = context;
    }

    void createNotification()
    {

        Bitmap bigImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.verse_img_2);
        Bitmap thumbnail = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher);

        Intent intent = new Intent(mContext , MainActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                100 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);




        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(R.drawable.ic_prayer_request);
        mBuilder.setContentTitle("Verse of the Day!")
                .setContentText("Click on the 'Notification' to read the verse!")
                .setAutoCancel(false)
                .setLargeIcon(thumbnail)
                .setAutoCancel(true)
                .setSound(null)
//                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigImage))
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setContentIntent(resultPendingIntent);

        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
        {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Verse of the day!", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        assert mNotificationManager != null;
        mNotificationManager.notify(100 /* Request Code */, mBuilder.build());
    }
}

主要活动代码

 public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Methods Call
        dailyVerseNotification();

}


    private void dailyVerseNotification() {

        Calendar time = Calendar.getInstance();
        time.setTimeZone(TimeZone.getTimeZone("GMT"));
        time.set(Calendar.HOUR_OF_DAY, 6);
        time.set(Calendar.MINUTE, 59);
        time.set(Calendar.AM_PM, Calendar.AM);
        time.set(Calendar.SECOND, 10);

        Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        assert alarmManager != null;
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}
android android-studio android-notifications
1个回答
1
投票

通过检查当前日期已解决。

Calendar calendar = Calendar.getInstance();
        //Setting time of the day (7 AM here) Notification will be sent everyday.
        Calendar currentDate = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 7);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 0);

        if(currentDate.after(calendar)){

            Log.w("Added a day", "1");
            calendar.add(Calendar.DATE, 1);
        }
© www.soinside.com 2019 - 2024. All rights reserved.