简体   繁体   English

Android Studio设置通知提醒(每周一次)-应用前一天

[英]Android studio setting notification reminder for app weekly - day before app

I have weekly notifications set up using the calendar and datepicker. 我有使用日历和日期选择器设置的每周通知。 However, this notification I am trying to set up uses a date from a database which is converted into milliseconds. 但是,我尝试设置的此通知使用的是数据库中的日期,该日期已转换为毫秒。 The variable which contains the milliseconds is used as the time to fire the reminder. 包含毫秒的变量用作触发提醒的时间。 However it will not work. 但是,它将不起作用。 The code does not throw any errors, th enotification simply doesnt fire. 该代码不会引发任何错误,根本不会触发通知。 My code is below. 我的代码如下。

MainActivity - used to trigger the notification MainActivity-用于触发通知

// Variable pulled from mysql with the app date
private String firstAppointment;

// Date of first app converted from String to Date format
private Date dateOfFirstAppointment;

// Date converted into milliseconds for the notification.
 private long appointmentInMilliseconds;

 /**
 * Method used to format the date from the MYSQL server
 */
private void getFirstAppointment() {

    String myDate = firstAppointment;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {
        dateOfFirstAppointment = sdf.parse(myDate);
        appointmentInMilliseconds = dateOfFirstAppointment.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

/**
 * Method which sets a weekly notification
 */
private void appointmentReminder() {

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlertReceiver2.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 5, intent, 0);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, appointmentInMilliseconds, pendingIntent);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, appointmentInMilliseconds, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
    Toast.makeText(this, "Appointment Reminder Set", Toast.LENGTH_SHORT).show();
}

Notification Class : 通知类别:

public class NotificationHelperChannel2 extends ContextWrapper {

public static final String channe2ID = "channe2ID";
public static final String channe2Name = "Channe2 Name";

private NotificationManager mManager;

public NotificationHelperChannel2(Context base) {
    super(base);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
    }
}

@TargetApi(Build.VERSION_CODES.O)
private void createChannel() {
    NotificationChannel channel = new NotificationChannel(channe2ID, channe2Name, NotificationManager.IMPORTANCE_HIGH);

    getManager2().createNotificationChannel(channel);
}

public NotificationManager getManager2() {
    if (mManager == null) {
        mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    return mManager;
}

public NotificationCompat.Builder getChannelNotification2() {
    return new NotificationCompat.Builder(getApplicationContext(), channe2ID)
            .setContentTitle("Appointment Reminder")
            .setContentText("You have an appointment tomorrow")
            .setSmallIcon(R.drawable.ic_notification_icon)
            .setLargeIcon(icon)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setVibrate(new long []{1000, 1000})
            .setLights(Color.RED, 3000, 3000)
            .setAutoCancel(true);

}

Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
        R.drawable.brain_meditate_icon);
}

Alert Receiver Class: 警报接收器类别:

public class AlertReceiver2 extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    NotificationHelperChannel2 notificationHelper2 = new NotificationHelperChannel2(context);
    NotificationCompat.Builder nb2 = notificationHelper2.getChannelNotification2();
    notificationHelper2.getManager2().notify(2, nb2.build());
}

} }

The notifications have different handling in different android operating system. 通知在不同的Android操作系统中具有不同的处理方式。 Possible that the alarm wont work because you are probably using a Pie, Oreo or at least Nougat Android in which requires a special handling 可能是因为您使用的是Pie,Oreo或至少是Nougat Android(需要特殊处理)的警报无法正常工作

Here is my code in handling notification in Oreo, Pie and Nougat. 这是我在Oreo,Pie和Nougat中处理通知的代码。

Might help you out. 可能会帮助您。

      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            int notificationId = 1;
            String channelId = "channel-01";
            String channelName = "Channel Name";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);
                notificationManager.createNotificationChannel(mChannel);
            }

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
                    .setSmallIcon(R.drawable.ic_check)
                    .setContentTitle("Branch Checklist")
                    .setContentText(resultValue)
                    .setVibrate(new long[]{100, 250})
                    .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(resultValue));

//        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
//        stackBuilder.addNextIntent(intent);
//        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
//                0,
//                PendingIntent.FLAG_UPDATE_CURRENT
//        );
//        mBuilder.setContentIntent(resultPendingIntent);

            notificationManager.notify(notificationId, mBuilder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM