Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tgs.app.submission4aplikasilocalstorage.notification;
- import android.app.AlarmManager;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.BitmapFactory;
- import android.os.Build;
- import android.util.Log;
- import androidx.core.app.NotificationCompat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import tgs.app.submission4aplikasilocalstorage.R;
- import tgs.app.submission4aplikasilocalstorage.entity.Movie;
- public class MovieReleaseTodayReminder extends BroadcastReceiver {
- private static int notifId;
- private static final String NOTIF_ID = "notif_Id";
- // String id;
- // String id_movie;
- String title_movie;
- // String release_movie;
- // String score_movie;
- // String overview_movie;
- // String image;
- private static final String CHANNEL_ID = "channel_02";
- private static final String CHANNEL_NAME = "release today reminder";
- @Override
- public void onReceive(Context context, Intent intent) {
- String movie_title = intent.getStringExtra(title_movie);
- notifId = intent.getIntExtra(NOTIF_ID, 0);
- Log.e("JudulReleas", movie_title);
- String desc = movie_title + " rilis hari ini";
- sendNotification(context, desc, notifId);
- }
- public void sendNotification(Context context, String desc, int notifId){
- NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
- .setSmallIcon(R.drawable.ic_movie)
- .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_notifications))
- .setContentTitle("Movie Catalogue")
- .setContentText(desc);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
- CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
- builder.setChannelId(CHANNEL_ID);
- if (notificationManager != null){
- notificationManager.createNotificationChannel(channel);
- }
- }
- Notification notification = builder.build();
- if (notificationManager != null){
- notificationManager.notify(notifId, notification);
- }
- }
- public void setAlarm(Context context, ArrayList<Movie> movie) {
- for (int i=0; i < movie.size(); i++) {
- AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(context, MovieReleaseTodayReminder.class);
- intent.putExtra(title_movie, movie.get(i).getMovie_title());
- intent.putExtra(NOTIF_ID, i);
- Log.e("TodayReleaseLoop", movie.get(i).getMovie_title());
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, 8);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 100, intent, 0);
- if (alarmManager != null) {
- alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment