SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package github.io.rahmatsyam.alarammanager;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.media.RingtoneManager;
  11.  
  12. import androidx.core.app.NotificationCompat;
  13.  
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import java.util.Date;
  17.  
  18. public class AppReceiver extends BroadcastReceiver {
  19.  
  20.     PendingIntent pendingIntent;
  21.     private static final int ALARM_REQUEST_CODE = 134;
  22.     //set interval notifikasi 10 detik
  23.     int interval_seconds = 10;
  24.     NotificationManager alarmNotificationManager;
  25.     String NOTIFICATION_CHANNEL_ID = "rasupe_channel_id";
  26.     String NOTIFICATION_CHANNEL_NAME = "rasupe channel";
  27.     int NOTIFICATION_ID = 1;
  28.  
  29.  
  30.     @Override
  31.     public void onReceive(Context context, Intent intent) {
  32.  
  33.         Intent alarmIntent = new Intent(context, AppReceiver.class);
  34.         pendingIntent = PendingIntent.getBroadcast(context, ALARM_REQUEST_CODE, alarmIntent, 0);
  35.  
  36.         int hour = intent.getIntExtra("HOUR", 0);
  37.         int minute = intent.getIntExtra("MINUTE", 0);
  38.  
  39.         Calendar calendar = Calendar.getInstance();
  40.         calendar.setTimeInMillis(System.currentTimeMillis());
  41.         calendar.add(Calendar.HOUR_OF_DAY, 13);
  42.         calendar.add(Calendar.MINUTE, 17);
  43.         if (intent.getAction() != null) {
  44.             AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  45.  
  46.             if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED") && alarmManager != null) {
  47.  
  48.                 alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
  49.  
  50.             }
  51.  
  52.         }
  53.  
  54.         sendNotification(context, intent);
  55.  
  56.     }
  57.  
  58.     private void sendNotification(Context context, Intent intent) {
  59.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
  60.         String dateTime = simpleDateFormat.format(new Date());
  61.  
  62.         String notif_title = "Test 1";
  63.         String notif_content = "Notif time " + dateTime;
  64.         alarmNotificationManager = (NotificationManager) context
  65.                 .getSystemService(Context.NOTIFICATION_SERVICE);
  66.  
  67.         intent = new Intent(context, MainActivity.class);
  68.         intent.putExtra("notifkey", "notifvalue");
  69.  
  70.         PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
  71.                 intent, PendingIntent.FLAG_UPDATE_CURRENT);
  72.  
  73.         //cek jika OS android Oreo atau lebih baru
  74.         //kalau tidak di set maka notifikasi tidak akan muncul di OS tersebut
  75.         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  76.             int importance = NotificationManager.IMPORTANCE_HIGH;
  77.             NotificationChannel mChannel = new NotificationChannel(
  78.                     NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
  79.             alarmNotificationManager.createNotificationChannel(mChannel);
  80.         }
  81.  
  82.         //Buat notification
  83.         NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
  84.         alamNotificationBuilder.setContentTitle(notif_title);
  85.         alamNotificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
  86.         alamNotificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
  87.         alamNotificationBuilder.setContentText(notif_content);
  88.         alamNotificationBuilder.setAutoCancel(true);
  89.         alamNotificationBuilder.setContentIntent(contentIntent);
  90.  
  91.         //Tampilkan notifikasi
  92.         alarmNotificationManager.notify(NOTIFICATION_ID, alamNotificationBuilder.build());
  93.     }
  94.  
  95.  
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top