Cześć. Mam pytanie, ze względu na fakt, że Android wprowadził ostatnio sporo zmian jeśli chodzi o notyfikacje, większość tutoriali rozwiązujących mój problem nie działa. Chodzi o notyfikację, którą użytkownik otrzymywałby raz dziennie o określonej godzinie, np codziennie o 20:00.
Znalazłem kod programistów Google, jednak oni włączają notyfikację poprzez naciśnięcie przycisku, nigdzie nie tłumaczą jak to zrobić, by notyfikacja odpalała się o określonej godzinie
MainActivity
package com.android.fundamentals.standup;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
/**
* MainActivity for the Stand up! app. Contains a toggle button that
* sets an alarm which delivers a Stand up notification every 15 minutes.
*/
public class MainActivity extends AppCompatActivity {
// Notification ID.
private static final int NOTIFICATION_ID = 0;
// Notification channel ID.
private static final String PRIMARY_CHANNEL_ID =
"primary_notification_channel";
private NotificationManager mNotificationManager;
/**
* Initializes the activity.
*
* @param savedInstanceState The current state data.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
ToggleButton alarmToggle = findViewById(R.id.alarmToggle);
// Set up the Notification Broadcast Intent.
Intent notifyIntent = new Intent(this, AlarmReceiver.class);
boolean alarmUp = (PendingIntent.getBroadcast(this, NOTIFICATION_ID,
notifyIntent, PendingIntent.FLAG_NO_CREATE) != null);
alarmToggle.setChecked(alarmUp);
final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
final AlarmManager alarmManager = (AlarmManager) getSystemService
(ALARM_SERVICE);
// Set the click listener for the toggle button.
alarmToggle.setOnCheckedChangeListener
(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged
(CompoundButton buttonView, boolean isChecked) {
String toastMessage;
if (isChecked) {
long repeatInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
long triggerTime = SystemClock.elapsedRealtime()
+ repeatInterval;
// If the Toggle is turned on, set the repeating alarm with
// a 15 minute interval.
if (alarmManager != null) {
alarmManager.setInexactRepeating
(AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerTime, repeatInterval,
notifyPendingIntent);
}
// Set the toast message for the "on" case.
toastMessage = getString(R.string.alarm_on_toast);
} else {
// Cancel notification if the alarm is turned off.
mNotificationManager.cancelAll();
if (alarmManager != null) {
alarmManager.cancel(notifyPendingIntent);
}
// Set the toast message for the "off" case.
toastMessage = getString(R.string.alarm_off_toast);
}
// Show a toast to say the alarm is turned on or off.
Toast.makeText(MainActivity.this, toastMessage,
Toast.LENGTH_SHORT).show();
}
});
// Create the notification channel.
createNotificationChannel();
}
/**
* Creates a Notification channel, for OREO and higher.
*/
public void createNotificationChannel() {
// Create a notification manager object.
mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Notification channels are only available in OREO and higher.
// So, add a check on SDK version.
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {
// Create the NotificationChannel with all the parameters.
NotificationChannel notificationChannel = new NotificationChannel
(PRIMARY_CHANNEL_ID,
"Stand up notification",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("Notifies every 15 minutes to " +
"stand up and walk");
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
}
AlarmReceiver
package com.android.fundamentals.standup;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
/**
* Broadcast receiver for the alarm, which delivers the notification.
*/
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";
/**
* Called when the BroadcastReceiver receives an Intent broadcast.
*
* @param context The Context in which the receiver is running.
* @param intent The Intent being received.
*/
@Override
public void onReceive(Context context, Intent intent) {
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
// Deliver the notification.
deliverNotification(context);
}
/**
* Builds and delivers the notification.
*
* @param context, activity context.
*/
private void deliverNotification(Context context) {
// 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.ic_stand_up)
.setContentTitle(context.getString(R.string.notification_title))
.setContentText(context.getString(R.string.notification_text))
.setContentIntent(contentPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL);
// Deliver the notification
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
}
}
Czy macie może pomysł, jakie zmiany trzeba tu wprowadzić, by notyfikacja odpalała się automatycznie po włączeniu aplikacji po raz pierwszy i ustawiała się na odpalenie po raz pierwszy o 20:00, a następnie co 24 godziny ?