web-dev-qa-db-fra.com

Implémenter développer et réduire notification android

Je dois implémenter les notifications de développement et de réduction dans la barre d'état pour Android 4.0 et versions ultérieures. J'ai effectué une recherche sur Google mais je n'ai trouvé aucune solution de code pour la mise en œuvre.

Merci d'avance

27
Pratik

Une variable Notification extensible est un cas particulier de NotificationBig View . Si le Big View ne se trouve pas en haut du tiroir de notification, il est affiché comme étant «fermé» et peut être étendu par balayage. Citation de développeurs Android:

La vue agrandie d'une notification apparaît uniquement lorsque la notification est développée, ce qui se produit lorsque la notification se trouve en haut du tiroir de notification ou lorsque l'utilisateur développe la notification d'un geste. Les notifications étendues sont disponibles à partir d'Android 4.1.

La Big ViewNotification peut être créée comme suit:

Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();

ou

Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
  BitmapFactory.decodeResource(getResources(),
    R.drawable.my_picture)).build();

Ici est un tutoriel.

40
Gunnar Karlsson
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();

Vous changez 

.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))

avec l'annonce OK !!! 

notification = new NotificationCompat.Builder(context)

Voici un exemple: 

 enter image description here Vous pouvez définir le code 

Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new     NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);

notification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(th_title)
    .setContentText(th_alert)
    .setAutoCancel(true)
 // .setStyle(new Notification.BigTextStyle().bigText(th_alert)  ตัวเก่า
 // .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
    .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
    .setContentIntent(pendingIntent)
    .setNumber(++numMessages)
    .build();

notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);

ou 

 private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
               // .setContentTitle(notification.getTitle())
                .setContentTitle(getResources().getText(R.string.app_name))
                .setContentText(notification.getBody())
                .setAutoCancel(true)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
                .setContentInfo(notification.getTitle())
                .setLargeIcon(icon)
                .setColor(Color.RED)
                .setSmallIcon(R.drawable.logo);

        try {
            String picture_url = data.get("picture_url");
            if (picture_url != null && !"".equals(picture_url)) {
                URL url = new URL(picture_url);
                Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                notificationBuilder.setStyle(
                        new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
                );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        notificationBuilder.setLights(Color.YELLOW, 1000, 300);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
33
Pong Petrung

Nous ne pouvons pas créer de notification extensible dans les versions inférieures à Android 4.1 . Mais au lieu de cela, nous pouvons empiler les notifications, puis définir une intention en attente pour notre activité assez personnalisée qui affiche toutes les notifications de la liste . L'utilisateur sera heureux de voir ceci :)

0
g4gaj