0% found this document useful (0 votes)
8 views6 pages

NOTIFICACIONES

The document contains an Android application manifest and related code for handling notifications via Firebase. It includes permissions for internet access and posting notifications, along with a main activity that requests notification permissions and retrieves a Firebase token. Additionally, it defines a service for receiving Firebase messages and a layout for user input, alongside a custom app theme.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

NOTIFICACIONES

The document contains an Android application manifest and related code for handling notifications via Firebase. It includes permissions for internet access and posting notifications, along with a main activity that requests notification permissions and retrieves a Firebase token. Additionally, it defines a service for receiving Firebase messages and a layout for user input, alongside a custom app theme.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ANDROID MANIFEST

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp"
tools:targetApi="31">

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!-- Servicio para recibir notificaciones de Firebase -->


<service
android:name=".MyFirebaseMessagingService"
android:exported="true"
android:permission="TODO">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>

</application>
</manifest>
MAINACTIVITY

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.google.firebase.messaging.FirebaseMessaging

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Solicitar permiso en Android 13+


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
1
)
}
}

// Obtener Token de Firebase


FirebaseMessaging.getInstance().token
.addOnCompleteListener { task ->
if (!task.isSuccessful) {
return@addOnCompleteListener
}
val token = task.result
println("Token de FCM: $token")
}
}
}
MyFirebaseMessagingService

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onNewToken(token: String) {


super.onNewToken(token)
Log.d("FCM Token", "Nuevo token: $token")
}

override fun onMessageReceived(remoteMessage: RemoteMessage) {


super.onMessageReceived(remoteMessage)

Log.d("FCM Notificación", "Mensaje recibido: ${remoteMessage.data}")

// Si el mensaje tiene notificación


remoteMessage.notification?.let {
Log.d("FCM Notificación", "Título: ${it.title}, Mensaje: ${it.body}")
mostrarNotificacion(it.title ?: "Notificación", it.body ?: "Mensaje recibido")
}
}

private fun mostrarNotificacion(title: String, message: String) {


val channelId = "my_channel_id"
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

val builder = NotificationCompat.Builder(this, channelId)


.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager

// Crear canal de notificación en Android 8+


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId, "Canal Notificaciones",
NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}

notificationManager.notify(0, builder.build())
}
}
ACTIVITY_MAIN.XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Número de teléfono"
android:inputType="phone" />

<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPhoneNumber"
android:layout_marginTop="16dp"
android:hint="Mensaje"
android:inputType="textMultiLine" />

<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextMessage"
android:layout_marginTop="16dp"
android:text="Enviar SMS" />
</RelativeLayout>
Theme.MyApp

<resources xmlns:tools="http://schemas.android.com/tools">

<style name="Theme.MyApp" parent="Theme.Material3.DayNight.NoActionBar">


<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<item name="android:windowBackground">@color/black</item>
</style>

</resources>

You might also like