Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

java - Send a notification every year at a specific time even when the phone has restarted

I'm developing my first application and I've been blocking on a key app's feature for several weeks now.

On this application, users have the possibility to record annual events and receive 1 reminder on the day of the event every year.

During my tests, it works when the reminder date is today but it doesn't work when the reminder date is another day. And I also don't know how to repeat this reminder every year on the anniversary date of the event.

Below are sample codes:

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

private NotificationManager mNotificationManager;
// Notification ID.
private static final int NOTIFICATION_ID = 0;
// Notification channel ID.
private static final String PRIMARY_CHANNEL_ID =
        "primary_notification_channel";

@Override
public void onReceive(Context context, Intent intent) {
    mNotificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notificationId = intent.getIntExtra("notificationId",0);
    String nomEvent = intent.getStringExtra("nomEvent");

    // Create the content intent for the notification, which launches
    // this activity


    Intent contentIntent = new Intent(context, MainActivity.class);

    PendingIntent contentPendingIntent = PendingIntent.getActivity
            (context, NOTIFICATION_ID, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Build the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder
            (context, PRIMARY_CHANNEL_ID)
            .setSmallIcon(R.drawable.alerter_ic_notifications)
            .setContentTitle("Rappel")
            .setContentText(nomEvent)
            .setContentIntent(contentPendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_REMINDER)
            .setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL);

    // Deliver the notification
    mNotificationManager.notify(notificationId, builder.build());
}

AddNewEventActivity.java I decided to save the notification when adding the event from the AddNewEvent activity. I don't know if that's correct...

public class AddNewEventActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

private NotificationManager mNotificationManager;


    @Override
protected void onCreate(Bundle savedInstanceState) { ...
        mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    // Set up the Notification Broadcast Intent.
    Intent notifyIntent = new Intent(this, AlarmReceiver.class);
    notifyIntent.putExtra("notificationId", NOTIFICATION_ID);
    notifyIntent.putExtra("nomEvent", mEditTitle.getText().toString());

    Button saveButton =(Button) findViewById(R.id.saveBtn);
    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {...


            // Set the alarm to start at approximately 8:00 a.m.
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MONTH, monthEvent);
            calendar.set(Calendar.DAY_OF_MONTH, dayEvent);
            calendar.set(Calendar.YEAR, yearEvent);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                                             calendar.getTimeInMillis(),
                                             AlarmManager.INTERVAL_DAY,
                                             notifyPendingIntent);

AndroidManifest.xml

<receiver android:name=".util.AlarmReceiver" android:enabled="true" android:exported="false" />

Can someone help me how to solve this problem?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...