Tutoriales gratuitos para el aprendizaje de la programacion informatica! Recuerda que si lo puedes imaginar... lo puedes programar!

Crear notificaciones en Android




En el Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lvsistemas.mynotificacion">

<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/AppTheme">

<activity android:name=".EstrellasActivity"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>

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

</manifest>

En el MainActivity:


package com.lvsistemas.mynotificacion;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private PendingIntent pendingIntent;
private final static String CHANNEL_ID="NOTIFICACION";
public final static int NOTIFICACION_ID=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button notificacion = (Button) findViewById(R.id.buttonNotificacion);

notificacion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

irActivity();
crearCanalNotificacion();
crearNotificacion();

}});

}

private void irActivity() {

Intent intent = new Intent(this,EstrellasActivity.class);
TaskStackBuilder stackBuilder=TaskStackBuilder.create(this);
stackBuilder.addParentStack(EstrellasActivity.class);
stackBuilder.addNextIntent(intent);
pendingIntent=stackBuilder.getPendingIntent(1,PendingIntent.FLAG_UPDATE_CURRENT);

}

private void crearCanalNotificacion() {

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

CharSequence name="Notificacion";
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,name, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);}

}

private void crearNotificacion() {

NotificationCompat.Builder builder=new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
builder.setSmallIcon(R.drawable.ic_star_black_24dp);
builder.setContentTitle("Goduria Food");
builder.setContentText("Has recibido nuevas estrellas!");
builder.setColor(Color.BLUE);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
builder.setLights(Color.MAGENTA,1000,1000);
builder.setVibrate(new long[]{1000,1000,1000,1000,1000});
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setContentIntent(pendingIntent);

NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(NOTIFICACION_ID,builder.build());

}

}

Xml del MainActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">


<Button
android:id="@+id/buttonNotificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/notificacion" />
</LinearLayout>

Codigo del Activity donde abrira la notificacion (en mi caso se llama EstrellasActivity)

package com.lvsistemas.mynotificacion;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import static com.lvsistemas.mynotificacion.MainActivity.NOTIFICACION_ID;
import android.os.Bundle;

public class EstrellasActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estrellas);

//borrar la notificacion
NotificationManagerCompat notificationManagerCompat=NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.cancel(NOTIFICACION_ID);
}
}

Xml de EstrellaActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".EstrellasActivity">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/notificacion" />
</LinearLayout>

No hay comentarios:

Publicar un comentario