จะให้การแจ้งเตือนบน Android ตามเวลาที่กำหนดใน Android Oreo ได้อย่างไร

ฉันกำลังมองหาวิธีสร้าง preference ใน Settings เพื่อส่งการแจ้งเตือน ณ เวลาที่กำหนดของวัน (ตั้งค่าโดยผู้ใช้ในการตั้งค่า) ในแอป Android ฉันได้ดูหัวข้อต่างๆเช่นสิ่งนี้ สิ่งนี้ใช้ไม่ได้กับ Android Oreo

ใครสามารถช่วยฉันในเรื่องนี้หรือชี้ให้ฉันดูบทช่วยสอน?


person Tricky Bay    schedule 14.07.2018    source แหล่งที่มา
comment
เนื่องจากไม่มีใครตอบกลับ...การแจ้งเตือนจำเป็นต้องส่งในเวลา เฉพาะ จริงๆ หรือไม่ ในหลายกรณีไม่ แต่ถ้าคำตอบคือใช่ AlarmManager.setAlarmClock ก็เป็นทางเลือกเดียวในท้องถิ่นที่สมจริง อีกวิธีหนึ่งคือการใช้ FCM ที่มีลำดับความสำคัญสูง: Firebase Cloud Message หากสามารถทนต่อความล่าช้าได้ถึง ~10 นาที บางอย่างเช่น AlarmManager.setExactAndAllowWhileIdle อาจมีประโยชน์ หรือดูว่าแอปของคุณสามารถทนต่อการใช้ JobScheduler ได้หรือไม่   -  person Elletlar    schedule 16.07.2018
comment
ใช่ ฉันต้องการส่งการแจ้งเตือนรายวันตามเวลาที่ผู้ใช้กำหนด Alarm Manager สามารถทำงานได้ในกรณีของฉัน แต่ฉันกำลังมองหาวิธีการนำไปใช้งาน บทช่วยสอนที่เหมาะกับ Android Oreo แน่นอน   -  person Tricky Bay    schedule 17.07.2018


คำตอบ (2)


หลังจากดูโพสต์ต่างๆ และค้นคว้าเกี่ยวกับการใช้งาน AlarmManager แล้ว นี่คือสิ่งที่ใช้ได้ผลสำหรับฉัน

พื้นฐานสำหรับสิ่งนี้คือนี้โพสต์และกำหนดเวลาการเตือนซ้ำ Android เอกสารประกอบ

นี่คือการใช้งานปัจจุบันของฉัน:

ฉันมี SwitchPreference และการใช้งาน TimePicker คือ Settings

SwitchPreference เพื่อถามว่าผู้ใช้ต้องการเปิดใช้งานการแจ้งเตือนซ้ำรายวันหรือไม่

TimePicker เพื่อตั้งเวลาการแจ้งเตือน

ในวิธีการ OnCreate ของ MainActivity หรือที่ใดก็ตามที่คุณกำลังอ่าน SharedPreferences ให้ทำดังนี้:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean dailyNotify = sharedPref.getBoolean(SettingsActivity.KEY_PREF_DAILY_NOTIFICATION, true);
PackageManager pm = this.getPackageManager();
ComponentName receiver = new ComponentName(this, DeviceBootReceiver.class);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// if user enabled daily notifications
if (dailyNotify) {
    //region Enable Daily Notifications
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
    calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
    calendar.set(Calendar.SECOND, 1);
    // if notification time is before selected time, send notification the next day
    if (calendar.before(Calendar.getInstance())) {
        calendar.add(Calendar.DATE, 1);
    }
    if (manager != null) {
        manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_DAY, pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }
    }
    //To enable Boot Receiver class
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    //endregion
} else { //Disable Daily Notifications
    if (PendingIntent.getBroadcast(this, 0, alarmIntent, 0) != null && manager != null) {
        manager.cancel(pendingIntent);
        //Toast.makeText(this,"Notifications were disabled",Toast.LENGTH_SHORT).show();
    }
    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

ถัดไปเพิ่มคลาส AlarmReceiver ที่ใช้ BroadcastReceiver ดังนี้:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));
    SharedPreferences.Editor sharedPrefEditor = prefs.edit();

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(context, MainActivity.class);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingI = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("default",
                    "Daily Notification",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("Daily Notification");
            if (nm != null) {
                nm.createNotificationChannel(channel);
            }
        }
        NotificationCompat.Builder b = new NotificationCompat.Builder(context, "default");
        b.setAutoCancel(true)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher_foreground)
                .setTicker("{Time to watch some cool stuff!}")
                .setContentTitle("My Cool App")
                .setContentText("Time to watch some cool stuff!")
                .setContentInfo("INFO")
                .setContentIntent(pendingI);

        if (nm != null) {
            nm.notify(1, b.build());
            Calendar nextNotifyTime = Calendar.getInstance();
            nextNotifyTime.add(Calendar.DATE, 1);
            sharedPrefEditor.putLong("nextNotifyTime", nextNotifyTime.getTimeInMillis());
            sharedPrefEditor.apply();
        }
    }
}

ระบบจะปิด AlarmManager หากอุปกรณ์ปิดหรือรีบูต ดังนั้นให้รีสตาร์ทอีกครั้งใน BOOT COMPLETE เพิ่มคลาสนี้:

public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) {
        // on device boot complete, reset the alarm
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Objects.requireNonNull(context));

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, sharedPref.getInt("dailyNotificationHour", 7));
        calendar.set(Calendar.MINUTE, sharedPref.getInt("dailyNotificationMin", 15));
        calendar.set(Calendar.SECOND, 1);

        Calendar newC = new GregorianCalendar();
        newC.setTimeInMillis(sharedPref.getLong("nextNotifyTime", Calendar.getInstance().getTimeInMillis()));

        if (calendar.after(newC)) {
            calendar.add(Calendar.HOUR, 1);
        }

        if (manager != null) {
            manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    AlarmManager.INTERVAL_DAY, pendingIntent);
        }
    }
}
}

และสุดท้ายอย่าลืมเพิ่มการอนุญาตเหล่านี้ใน AndroidManidest:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

และลงทะเบียนผู้รับของคุณใน AndroidManifest

<application
    <!--YOUR APPLICATION STUFF-->

    <receiver android:name=".DeviceBootReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".AlarmReceiver" />

Notification ควรตั้งค่าตามเวลาที่กำหนดของวันที่ระบุโดย TimePicker และหากผู้ใช้เปิดใช้งาน SwitchPreference

person Tricky Bay    schedule 02.08.2018

มีตัวอย่างมากมายเกี่ยวกับวิธีการทำเช่นนี้บน StackOverflow แต่น่าเสียดายที่ไม่มีตัวอย่างใดทำงานอีกต่อไป เนื่องจาก Google เปลี่ยนวิธีการทำงานของ AlarmManager

  • Android 6: หลับใน
  • Android 7: Aggressive Doze [ปิดการใช้งาน CPU ไม่นานหลังจากปิดหน้าจอ]
  • Android 8: ข้อจำกัดเพิ่มเติมเกี่ยวกับบริการพื้นหลัง

ตัวเลือก เท่านั้น AlarmManager ที่ช่วยให้นักพัฒนาสามารถปลุกอุปกรณ์ในเวลาที่กำหนดได้คือ AlarmManager.setAlarmClock

การใช้ setAlarmClock

นอกจากการปลุกแล้ว คุณยังสามารถตั้งค่าเจตนาที่จะช่วยให้คุณสามารถเปิดแอปได้เมื่อมีการคลิกการปลุก

อุปกรณ์สามารถถูกปลุกในเวลาที่กำหนดได้โดยใช้ FCM ที่มีลำดับความสำคัญสูง, Firebase Cloud Message

แต่โดยสรุป ไม่มีตัวเลือกอื่น: 'setExact' และวิธีการที่เกี่ยวข้องจะไม่ทำงานตามชื่อที่โฆษณาอีกต่อไป

person Elletlar    schedule 17.07.2018