Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nukasoft.ayuntamientosnukasoft.FCM;
- import android.annotation.SuppressLint;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Build;
- import android.support.v4.app.NotificationCompat;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import com.nukasoft.ayuntamientosnukasoft.Display;
- import java.util.Map;
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- final static String NOTIFICATION_CHANNEL_ID = "NukaNoti";
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- if(remoteMessage.getData() != null)
- sendNotification(remoteMessage);
- }
- private void sendNotification(RemoteMessage remoteMessage) {
- Map<String, String> data = remoteMessage.getData();
- String title = data.get("title");
- String content = data.get("content");
- String aviso = "enviadaNoti";
- NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notificacion", NotificationManager.IMPORTANCE_MAX);
- notificationChannel.setDescription("Canal para las notificaciones");
- notificationChannel.enableLights(true);
- notificationChannel.setLightColor(Color.RED);
- notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
- notificationChannel.enableVibration(true);
- notificationManager.createNotificationChannel(notificationChannel);
- }
- Intent intent = new Intent(getApplicationContext(), Display.class);
- intent.putExtra("title", title);
- intent.putExtra("content", content);
- intent.putExtra("aviso", aviso);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
- notificationBuilder.setAutoCancel(true)
- .setDefaults(Notification.DEFAULT_ALL)
- .setWhen(System.currentTimeMillis())
- .setSmallIcon(android.support.v4.R.drawable.notification_icon_background)
- .setTicker("Notificacion")
- .setContentTitle(title)
- .setContentText(content)
- .setContentIntent(pendingIntent)
- .setContentInfo("info");
- notificationManager.notify(1, notificationBuilder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement