Notifications on android

0

Hi, I want my application to send a notification when I touch a button but it does not happen I do not know what I did wrong. I'm using this (work with sketchware):

Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
mBuilder.setSmallicon(R.drawable.ic_android_black);
mBuilder.setContentTitle("titulo");
mBuilder.setContentText("mensaje");
mBuilder.setDefaults( Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContent(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    
asked by CarlosSera 11.10.2018 в 00:15
source

2 answers

0

You have not called the notify event:

notificationManager.notify(1,mBuilder.build());

As a parameter, it receives the id of the notification that can be any integer, and Notification.Builder

I give you an example of how to create a personalized notification, to put your own sound, icon, effects, etc.

  private void showNotification(String cabezera, String cuerpo,String id_grupo){
    ///Envio de valores por El Bundle
    Intent i = new Intent(this,ChatGrupo.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("key_grupo",id_grupo);
    i.putExtra("label_name",cabezera);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i, PendingIntent.FLAG_ONE_SHOT);
    int icon = R.mipmap.ic_launcher;
    Uri soundNotification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setAutoCancel(true);
    builder.setContentTitle(cabezera);
    builder.setContentText(cuerpo);
    builder.setPriority(Notification.PRIORITY_LOW);
    builder.setSound(soundNotification);
    builder.setSmallIcon(icon);
    builder.setContentInfo("Chat");
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setTicker(cuerpo);
    builder.setContentIntent(pendingIntent);


    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    ///Random devuelve un numero entero Aleatorio con su metodo nexInt()
    ////esto ayudara a que cada notificacion sea unica y no sea pisada por una nueva.
    Random random = new Random();

    notificationManager.notify(random.nextInt(),builder.build());



}
    
answered by 12.10.2018 / 00:59
source
0

Thanks already solved the right thing will be:

Notification.Builder mBuilder = new Notification.Builder (MainActivity.this); mBuilder.setSmallIcon (R.drawable.ic_cloud_done_white); mBuilder.setContentTitle ("You have a connection"); mBuilder.setContentText ("You already have an internet connection"); mBuilder.setDefaults (Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

NotificationManager notificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent (getApplicationContext (), MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity (getApplicationContext (), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

mBuilder.setContentIntent (pendingIntent) .setAutoCancel (true); notificationManager.notify (1, mBuilder.build ());

    
answered by 12.10.2018 в 22:22