У меня локальное уведомление, которое я повторяю каждый день в определенное время. Я пробовал тестировать на нескольких устройствах, и это работает только на нескольких устройствах. Пожалуйста, проверьте код ниже и пролите немного света. Я пробовал с устройствами Samsung и LG.
Ниже мой AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_NOTIFICATION_URI);
mediaPlayer.start();
NotificationManager manager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.logo)
//example for large icon
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setContentTitle("Hey")
.setContentText("Did you read today")
.setOngoing(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
Intent i = new Intent(context, VerseActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(
context,
0,
i,
PendingIntent.FLAG_ONE_SHOT
);
// example for blinking LED
builder.setLights(0xFFb71c1c, 1000, 2000);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setContentIntent(pendingIntent);
manager.notify(12345, builder.build());
}
}
И моя основная активность
public class MainActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//Setting time of the day (8am here) when notification will be sent every day (default)
calendar.set(Calendar.HOUR_OF_DAY,15);
calendar.set(Calendar.MINUTE, 50);
SetAlarm(calendar.getTimeInMillis());
//alarm service
}
private void SetAlarm(long timeInMillis) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, AlarmManager.INTERVAL_DAY, broadcast);
}
}
Manifest.xml
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
/>