Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
- public static final String TAG = "NOTICIAS";
- @Override
- public void onTokenRefresh() {
- super.onTokenRefresh();
- String token = FirebaseInstanceId.getInstance().getToken();
- Log.d(TAG, "Token: " + token);
- enviarTokenAlServidor(token);
- }
- private void enviarTokenAlServidor(String token) {
- // Enviar token al servidor
- }
- }
- public class MyFirebaseMessagingService extends FirebaseMessagingService {
- public static final String TAG = "NOTICIAS";
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- String from = remoteMessage.getFrom();
- Log.d(TAG, "Mensaje recibido de: " + from);
- if (remoteMessage.getNotification() != null) {
- Log.d(TAG, "Notificaciรณn: " + remoteMessage.getNotification().getBody());
- mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
- }
- if (remoteMessage.getData().size() > 0) {
- Log.d(TAG, "Data: " + remoteMessage.getData());
- }
- }
- private void mostrarNotificacion(String title, String body) {
- //Intent intent = new Intent(this, PushActivity.class);
- Intent intent = new Intent(this, MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
- Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
- .setSmallIcon(R.drawable.icon)
- .setContentTitle(title)
- .setContentText(body)
- .setAutoCancel(true)
- .setSound(soundUri)
- .setContentIntent(pendingIntent);
- NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(0, notificationBuilder.build());
- }
- }
- public class PushActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_push);
- }
- }
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#e70d0d"
- tools:context=".firebase.PushActivity">
- </FrameLayout>
Add Comment
Please, Sign In to add comment