How to send Push notification [closed]

1

I'm currently checking when the user opens the app . First it checks to see if it is connected. If it is not, do not compare it. If it is connected, it compares the current version with the version that is in the Play Store.

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

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

Note: All the tutorials I've seen only show how to send via server, etc.

    
asked by anonymous 09.05.2017 / 19:19

1 answer

1

To do this use the NotificationCompat.Builder

Here's 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);
    
10.05.2017 / 15:59