Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class ServiceNotification extends Service {
  2.  
  3. DatabaseReference reference;
  4.  
  5. SharedPreferences preferences;
  6.  
  7. private NotificationManager notificationManager;
  8. private final static String CHANNEL_ID="NOTIFICACION";
  9. private final static int NOTIFICACION_ID=0;
  10. private PendingIntent pendingIntent;
  11.  
  12. public void onCreate(){
  13. super.onCreate();
  14.  
  15. // Crea la notificación y a que Activity quiero ir
  16. setPendingIntent();
  17. sendNotification();
  18.  
  19. }
  20.  
  21.  
  22. public int onStartCommand(Intent intent, int flags, int startId){
  23. return START_STICKY;
  24. }
  25.  
  26. @Override
  27. public IBinder onBind(Intent intent) {
  28. return null;
  29. }
  30.  
  31.  
  32. private void setPendingIntent(){
  33. Intent intent=new Intent(this, RecibirDelito.class);
  34. TaskStackBuilder stackBuilder= TaskStackBuilder.create(this);
  35. stackBuilder.addParentStack(RecibirDelito.class);
  36. stackBuilder.addNextIntent(intent);
  37. pendingIntent=stackBuilder.getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT);
  38. }
  39.  
  40.  
  41.  
  42. private void sendNotification() {
  43.  
  44. // Consulta la base de datos
  45. reference= FirebaseDatabase.getInstance().getReference();
  46. Query query = reference.child("delito");
  47.  
  48. // Evento cuando escucha si se añadió un nuevo delito
  49. query.addChildEventListener(new ChildEventListener() {
  50. @Override
  51. public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  52.  
  53. // Construye la notificación cuando se agrega un nuevo delito
  54. if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
  55. CharSequence name="Notifacion";
  56. NotificationChannel notificationChannel= new NotificationChannel(CHANNEL_ID,name,NotificationManager.IMPORTANCE_DEFAULT);
  57. notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  58. notificationManager.createNotificationChannel(notificationChannel);
  59.  
  60. }
  61.  
  62. NotificationCompat.Builder builder= new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
  63. builder.setSmallIcon(R.drawable.ic_android_black_24dp);
  64. builder.setContentTitle("Emergencia");
  65. builder.setContentText("Nueva emergencia");
  66. builder.setColor(Color.BLUE);
  67. builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
  68. builder.setLights(Color.MAGENTA,1000,1000);
  69. builder.setVibrate(new long[]{1000,1000,});
  70. builder.setDefaults(Notification.DEFAULT_SOUND);
  71.  
  72. builder.setContentIntent(pendingIntent);
  73.  
  74. NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
  75. notificationManagerCompat.notify(NOTIFICACION_ID,builder.build());
  76.  
  77.  
  78. }
  79.  
  80. @Override
  81. public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  82.  
  83. }
  84.  
  85. });
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement