Advertisement
SaniSanjaya

NotifReceiv

Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. package com.example.submission2.reminder;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.media.RingtoneManager;
  12. import android.net.Uri;
  13. import android.os.Build;
  14. import android.widget.Toast;
  15.  
  16. import androidx.core.app.NotificationCompat;
  17. import androidx.core.content.ContextCompat;
  18. import androidx.fragment.app.FragmentActivity;
  19. import androidx.lifecycle.LifecycleOwner;
  20. import androidx.lifecycle.Observer;
  21. import androidx.lifecycle.ViewModelProviders;
  22.  
  23. import com.example.submission2.MainActivity;
  24. import com.example.submission2.R;
  25.  
  26. import java.util.ArrayList;
  27. import java.util.Calendar;
  28.  
  29. public class NotificationReceiver extends BroadcastReceiver {
  30. public static final String EXTRA_MESSAGE = "message";
  31. public static final String EXTRA_TYPE = "type";
  32. public static final String EXTRA_TITLE = "title";
  33.  
  34.  
  35. private AlarmManager dailyReminder;
  36. private AlarmManager releaseReminder;
  37.  
  38. private PendingIntent dailyPendingIntent;
  39. private PendingIntent releasePendingIntent;
  40.  
  41. private ArrayList<ReleaseItem> myMovie = new ArrayList<>();
  42.  
  43. MovieReleaseToday releaseToday = new MovieReleaseToday();
  44.  
  45. private int movieid;
  46. private static final CharSequence CHANNEL_NAME = "Movie Catalogue";
  47. private final static String GROUP_KEY_MOVIE = "Movie";
  48. private static final int MAX_NOTIFICATION = 2;
  49.  
  50.  
  51. @Override
  52. public void onReceive(Context context, Intent intent) {
  53. // TODO: This method is called when the BroadcastReceiver is receiving
  54. // an Intent broadcast.
  55.  
  56. String type = intent.getStringExtra(EXTRA_TYPE);
  57. String message = intent.getStringExtra(EXTRA_MESSAGE);
  58. String title = intent.getStringExtra(EXTRA_TITLE);
  59. movieViewModel= ViewModelProviders.of(this).get(MovieViewModel.class);
  60. movieViewModel.getMovies().observe(this,getMovie);
  61. releaseToday.setMovieReleaseToday();
  62. //getMovie(context);
  63.  
  64. Toast.makeText(context, String.valueOf(myMovie.size()), Toast.LENGTH_SHORT).show();
  65. if (type.equals("1")) {
  66. showAlarmNotification(context, title, message);
  67.  
  68. } else if (type.equals("2")) {
  69.  
  70. if (!myMovie.isEmpty()) {
  71. showReleaseNotification(context);
  72. } else {
  73. showReleaseNoNotification(context, title, message);
  74. }
  75. }
  76.  
  77. }
  78.  
  79.  
  80. public void showAlarmNotification(Context context, String title, String message) {
  81. String CHANNEL_ID = "Channel_1";
  82. String CHANNEL_NAME = "AlarmManager channel";
  83. NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  84. Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  85. NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
  86. .setSmallIcon(R.drawable.ic_notifications_white_24dp)
  87. .setContentTitle(title)
  88. .setContentText(message)
  89. .setColor(ContextCompat.getColor(context, android.R.color.transparent))
  90. .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
  91. .setSound(alarmSound);
  92. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  93. NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
  94. CHANNEL_NAME,
  95. NotificationManager.IMPORTANCE_DEFAULT);
  96. channel.enableVibration(true);
  97. channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
  98. builder.setChannelId(CHANNEL_ID);
  99. if (notificationManagerCompat != null) {
  100. notificationManagerCompat.createNotificationChannel(channel);
  101. }
  102. }
  103. Notification notification = builder.build();
  104. if (notificationManagerCompat != null) {
  105. notificationManagerCompat.notify(1, notification);
  106. }
  107.  
  108.  
  109. }
  110.  
  111. public void showReleaseNoNotification(Context context, String title, String message) {
  112. String CHANNEL_ID = "Channel_2";
  113. String CHANNEL_NAME = "AlarmManager2 channel";
  114. NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  115. Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  116. NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
  117. .setSmallIcon(R.drawable.ic_notifications_white_24dp)
  118. .setContentTitle(title)
  119. .setContentText(message)
  120. .setColor(ContextCompat.getColor(context, android.R.color.transparent))
  121. .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
  122. .setSound(alarmSound);
  123. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  124. NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
  125. CHANNEL_NAME,
  126. NotificationManager.IMPORTANCE_DEFAULT);
  127. channel.enableVibration(true);
  128. channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
  129. builder.setChannelId(CHANNEL_ID);
  130. if (notificationManagerCompat != null) {
  131. notificationManagerCompat.createNotificationChannel(channel);
  132. }
  133. }
  134. Notification notification = builder.build();
  135. if (notificationManagerCompat != null) {
  136. notificationManagerCompat.notify(1, notification);
  137. }
  138.  
  139.  
  140. }
  141.  
  142. public void setRepeatingAlarm(Context context, String title, String message) {
  143. dailyReminder = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  144.  
  145. Intent intentDaily = new Intent(context, NotificationReceiver.class);
  146. intentDaily.putExtra(EXTRA_MESSAGE, message);
  147. intentDaily.putExtra(EXTRA_TITLE, title);
  148. intentDaily.putExtra(EXTRA_TYPE, "1");
  149.  
  150. Calendar calendar = Calendar.getInstance();
  151. calendar.set(Calendar.HOUR_OF_DAY, 7);
  152. calendar.set(Calendar.MINUTE, 0);
  153. calendar.set(Calendar.SECOND, 0);
  154.  
  155. dailyPendingIntent = PendingIntent.getBroadcast(context, 1, intentDaily, 0);
  156. if (dailyReminder != null) {
  157. dailyReminder.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, dailyPendingIntent);
  158. }
  159. }
  160.  
  161. public void stopRepeatingAlarm(Context context) {
  162. dailyReminder.cancel(dailyPendingIntent);
  163. }
  164.  
  165. public void showReleaseNotification(Context context) {
  166.  
  167.  
  168. NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  169. NotificationCompat.Builder mBuilder;
  170. for (movieid = 1; movieid < myMovie.size(); movieid++) {
  171.  
  172.  
  173. //Melakukan pengecekan jika idNotification lebih kecil dari Max Notif
  174. String CHANNEL_ID = "channel_01";
  175. if (movieid < MAX_NOTIFICATION) {
  176. mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
  177. .setContentTitle(myMovie.get(movieid).getTitle())
  178. .setContentText(myMovie.get(movieid).getDesc())
  179.  
  180. .setSmallIcon(R.drawable.ic_notifications_white_24dp)
  181. .setGroup(GROUP_KEY_MOVIE)
  182. .setAutoCancel(true);
  183. } else {
  184. int jum = myMovie.size() - 2;
  185. NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle()
  186. .addLine(myMovie.get(movieid).getTitle() + " " + myMovie.get(movieid).getDesc())
  187. .addLine(myMovie.get(movieid - 1).getTitle() + " " + myMovie.get(movieid - 1).getDesc())
  188. .setBigContentTitle(movieid + 1 + " Movie release Today")
  189.  
  190. .setSummaryText("+" + jum + " more");
  191. //Toast.makeText(context, "type 2 " + myMovie.get(movieid).getTitle(), Toast.LENGTH_SHORT).show();
  192.  
  193. mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
  194. .setContentTitle(myMovie.get(movieid).getTitle())
  195. .setContentText(myMovie.get(movieid).getDesc())
  196. .setSmallIcon(R.drawable.ic_notifications_white_24dp)
  197. .setGroup(GROUP_KEY_MOVIE)
  198. .setGroupSummary(true)
  199. .setStyle(inboxStyle)
  200. .setAutoCancel(true);
  201.  
  202.  
  203. }
  204. /*
  205. Untuk android Oreo ke atas perlu menambahkan notification channel
  206. Materi ini akan dibahas lebih lanjut di modul extended
  207. */
  208. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  209.  
  210. /* Create or update. */
  211. NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
  212. CHANNEL_NAME,
  213. NotificationManager.IMPORTANCE_DEFAULT);
  214.  
  215. mBuilder.setChannelId(CHANNEL_ID);
  216.  
  217. if (mNotificationManager != null) {
  218. mNotificationManager.createNotificationChannel(channel);
  219. }
  220. }
  221.  
  222. Notification notification = mBuilder.build();
  223.  
  224. if (mNotificationManager != null) {
  225. mNotificationManager.notify(movieid, notification);
  226. }
  227. }
  228.  
  229. }
  230.  
  231. public void setMovieReleaseAlarm(Context context, String title, String message) {
  232. releaseReminder = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  233.  
  234. Intent intentRelease = new Intent(context, NotificationReceiver.class);
  235. intentRelease.putExtra(EXTRA_MESSAGE, message);
  236. intentRelease.putExtra(EXTRA_TITLE, title);
  237. intentRelease.putExtra(EXTRA_TYPE, "2");
  238.  
  239. Calendar calendar = Calendar.getInstance();
  240. calendar.set(Calendar.HOUR_OF_DAY, 8);
  241. calendar.set(Calendar.MINUTE, 0);
  242. calendar.set(Calendar.SECOND, 0);
  243.  
  244. releasePendingIntent = PendingIntent.getBroadcast(context, 2, intentRelease, 0);
  245. if (releaseReminder != null) {
  246. releaseReminder.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, releasePendingIntent);
  247. }
  248. }
  249.  
  250. public void stopMovieReleaseAlarm(Context context) {
  251. releaseReminder.cancel(releasePendingIntent);
  252. }
  253.  
  254.  
  255. private Observer<ArrayList<Movie>> getMovie= new Observer<ArrayList<Movie>>() {
  256.  
  257. @Override
  258. public void onChanged(ArrayList<Movie> movies) {
  259. if(movies!=null){
  260. myMovie = releaseItems;
  261. Toast.makeText(context, String.valueOf(myMovie.size()), Toast.LENGTH_SHORT).show();
  262.  
  263. }
  264. }
  265. };
  266.  
  267.  
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement