Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServiceNotification extends Service {
- DatabaseReference reference;
- SharedPreferences preferences;
- private NotificationManager notificationManager;
- private final static String CHANNEL_ID="NOTIFICACION";
- private final static int NOTIFICACION_ID=0;
- private PendingIntent pendingIntent;
- public void onCreate(){
- super.onCreate();
- // Crea la notificación y a que Activity quiero ir
- setPendingIntent();
- sendNotification();
- }
- public int onStartCommand(Intent intent, int flags, int startId){
- return START_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- private void setPendingIntent(){
- Intent intent=new Intent(this, RecibirDelito.class);
- TaskStackBuilder stackBuilder= TaskStackBuilder.create(this);
- stackBuilder.addParentStack(RecibirDelito.class);
- stackBuilder.addNextIntent(intent);
- pendingIntent=stackBuilder.getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT);
- }
- private void sendNotification() {
- // Consulta la base de datos
- reference= FirebaseDatabase.getInstance().getReference();
- Query query = reference.child("delito");
- // Evento cuando escucha si se añadió un nuevo delito
- query.addChildEventListener(new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot dataSnapshot, String s) {
- // Construye la notificación cuando se agrega un nuevo delito
- if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
- CharSequence name="Notifacion";
- NotificationChannel notificationChannel= new NotificationChannel(CHANNEL_ID,name,NotificationManager.IMPORTANCE_DEFAULT);
- notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- notificationManager.createNotificationChannel(notificationChannel);
- }
- NotificationCompat.Builder builder= new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
- builder.setSmallIcon(R.drawable.ic_android_black_24dp);
- builder.setContentTitle("Emergencia");
- builder.setContentText("Nueva emergencia");
- builder.setColor(Color.BLUE);
- builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
- builder.setLights(Color.MAGENTA,1000,1000);
- builder.setVibrate(new long[]{1000,1000,});
- builder.setDefaults(Notification.DEFAULT_SOUND);
- builder.setContentIntent(pendingIntent);
- NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
- notificationManagerCompat.notify(NOTIFICACION_ID,builder.build());
- }
- @Override
- public void onChildChanged(DataSnapshot dataSnapshot, String s) {
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement