Advertisement
Guest User

Pal taki

a guest
Jun 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. package com.nukasoft.ayuntamientosnukasoft.FCM;
  2.  
  3.  
  4. import android.annotation.SuppressLint;
  5. import android.app.Notification;
  6. import android.app.NotificationChannel;
  7. import android.app.NotificationManager;
  8. import android.app.PendingIntent;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.graphics.Color;
  12. import android.os.Build;
  13. import android.support.v4.app.NotificationCompat;
  14.  
  15. import com.google.firebase.messaging.FirebaseMessagingService;
  16. import com.google.firebase.messaging.RemoteMessage;
  17. import com.nukasoft.ayuntamientosnukasoft.Display;
  18.  
  19. import java.util.Map;
  20.  
  21. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  22.  
  23. final static String NOTIFICATION_CHANNEL_ID = "NukaNoti";
  24.  
  25. @Override
  26. public void onMessageReceived(RemoteMessage remoteMessage) {
  27. super.onMessageReceived(remoteMessage);
  28.  
  29. if(remoteMessage.getData() != null)
  30. sendNotification(remoteMessage);
  31.  
  32. }
  33.  
  34. private void sendNotification(RemoteMessage remoteMessage) {
  35.  
  36. Map<String, String> data = remoteMessage.getData();
  37. String title = data.get("title");
  38. String content = data.get("content");
  39. String aviso = "enviadaNoti";
  40.  
  41. NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
  42.  
  43. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  44.  
  45. @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notificacion", NotificationManager.IMPORTANCE_MAX);
  46.  
  47. notificationChannel.setDescription("Canal para las notificaciones");
  48. notificationChannel.enableLights(true);
  49. notificationChannel.setLightColor(Color.RED);
  50. notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
  51. notificationChannel.enableVibration(true);
  52.  
  53. notificationManager.createNotificationChannel(notificationChannel);
  54.  
  55. }
  56.  
  57. Intent intent = new Intent(getApplicationContext(), Display.class);
  58. intent.putExtra("title", title);
  59. intent.putExtra("content", content);
  60. intent.putExtra("aviso", aviso);
  61.  
  62. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  63. PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
  64.  
  65. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
  66.  
  67. notificationBuilder.setAutoCancel(true)
  68. .setDefaults(Notification.DEFAULT_ALL)
  69. .setWhen(System.currentTimeMillis())
  70. .setSmallIcon(android.support.v4.R.drawable.notification_icon_background)
  71. .setTicker("Notificacion")
  72. .setContentTitle(title)
  73. .setContentText(content)
  74. .setContentIntent(pendingIntent)
  75. .setContentInfo("info");
  76.  
  77. notificationManager.notify(1, notificationBuilder.build());
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement