Guest User

Untitled

a guest
May 21st, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
  2. public static final String TAG = "NOTICIAS";
  3.  
  4. @Override
  5. public void onTokenRefresh() {
  6. super.onTokenRefresh();
  7.  
  8. String token = FirebaseInstanceId.getInstance().getToken();
  9.  
  10. Log.d(TAG, "Token: " + token);
  11.  
  12. enviarTokenAlServidor(token);
  13. }
  14.  
  15. private void enviarTokenAlServidor(String token) {
  16. // Enviar token al servidor
  17. }
  18. }
  19.  
  20. public class MyFirebaseMessagingService extends FirebaseMessagingService {
  21.  
  22. public static final String TAG = "NOTICIAS";
  23.  
  24. @Override
  25. public void onMessageReceived(RemoteMessage remoteMessage) {
  26. super.onMessageReceived(remoteMessage);
  27.  
  28. String from = remoteMessage.getFrom();
  29. Log.d(TAG, "Mensaje recibido de: " + from);
  30.  
  31. if (remoteMessage.getNotification() != null) {
  32. Log.d(TAG, "Notificaciรณn: " + remoteMessage.getNotification().getBody());
  33. mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
  34. }
  35.  
  36. if (remoteMessage.getData().size() > 0) {
  37. Log.d(TAG, "Data: " + remoteMessage.getData());
  38. }
  39. }
  40.  
  41. private void mostrarNotificacion(String title, String body) {
  42.  
  43. //Intent intent = new Intent(this, PushActivity.class);
  44. Intent intent = new Intent(this, MainActivity.class);
  45. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  46. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
  47.  
  48. Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  49.  
  50. NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
  51. .setSmallIcon(R.drawable.icon)
  52. .setContentTitle(title)
  53. .setContentText(body)
  54. .setAutoCancel(true)
  55. .setSound(soundUri)
  56. .setContentIntent(pendingIntent);
  57.  
  58. NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  59. notificationManager.notify(0, notificationBuilder.build());
  60.  
  61. }
  62. }
  63.  
  64. public class PushActivity extends AppCompatActivity {
  65.  
  66. @Override
  67. protected void onCreate(Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. setContentView(R.layout.activity_push);
  70. }
  71. }
  72.  
  73. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  74. xmlns:app="http://schemas.android.com/apk/res-auto"
  75. xmlns:tools="http://schemas.android.com/tools"
  76. android:layout_width="match_parent"
  77. android:layout_height="match_parent"
  78. android:background="#e70d0d"
  79. tools:context=".firebase.PushActivity">
  80.  
  81. </FrameLayout>
Add Comment
Please, Sign In to add comment