How to send Push notification

Asked

Viewed 159 times

1

Currently I do a check when the user opens the app. First it checks if it is connected. If it is not, it does not compare. If connected, it compares the current version with the version in the Play Store.

So I send a Toast saying that the app is updated or has an update in the store.

Like, instead of this toast, send a notification push?

Obs.: all tutorials I’ve seen only show how to send via server, etc.

  • 4

    I think what you want is not a push notification, but a simple notifying, the kind that appears when you open the android notifications bar. Push notification is actually nothing more than information that the server sends to your program, it is your program that can then display or not an android notification.

1 answer

1


For this use if the Notificationcompat.Builder

Follow an example:

/**
 * Identificador da Notifição
 */
public static int ID_NOTIFICACAO = 9817;

private void gerarNotificacao(){
    /**
     * Vamos criar  um PendingIntent para abrir a url
     */
    String url = "https://play.google.com/store";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    final PendingIntent openUrl  =  PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);


    /**
     * Vamos criar uma acão para a Notificação
     */
    NotificationCompat.Action openAction = new NotificationCompat.Action(
            android.R.drawable.btn_default, // Imagem do botão
            "Abrir o Google play", // Texto do botão
            openUrl);

    /**
     * Vamos criar um NotificationCompat.Builder
     */
    // Criamos o builder da notificão
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            // titulo da Notificsação
            .setContentTitle("Atenção")
            // Imagem que será exibida na notificação
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
            //  Icone que será na barra de notificação do smartphone
            .setSmallIcon(R.drawable.ic_alert_notification)
            .setContentText("Existe uma atualização disponível no Google Play!")
            .addAction(openAction);

    /**
     * Pegamos o NotificationManager
     */
    NotificationManager manager = NotificationManager.class.cast(getSystemService(NOTIFICATION_SERVICE));

    /**
     * Exibimos a notificação
     */
    manager.notify(ID_NOTIFICACAO,builder.build());


}

To remove the notification:

NotificationManager.class.cast(getSystemService(NOTIFICATION_SERVICE)).cancel(ID_NOTIFICACAO);
  • 1

    Perfect. That’s exactly it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.