1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| package lakaj.notification.ci.notificationandroid;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
public class Activity extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int ID_NOTIFICATION = 1990;
//On recupere nos deux boutons en XML à partir de leur ID
final Button boutonCreateNotify = (Button) findViewById(R.id.createNotify);
Button boutonDeleteNotify = (Button) findViewById(R.id.deleteNotify);
//ajouter un ecouteur d'evenement
boutonCreateNotify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNotify();
}
});
boutonDeleteNotify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelNotify();
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
// la methode qui supprime la notification
private void cancelNotify() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//on supprime la notification grâce à son ID
notificationManager.cancel(1990);
}
// la methode qui crée la notification
private void createNotify() {
// creons un "gestionnaire de notification"
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// on crée la notification avec son icon et son texte defilant
Notification notification = new Notification(R.drawable.icon, "Toc toc c'est une notification ", System.currentTimeMillis());
//le PendingIntent c'est lui qui vas nous permettre d'atteindre notre deuxieme activity
//ActivityNotification sera ici notre deuxieme activité
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityNotification.class), 0);
//On definit le titre de la notification
String titreNotification = "C'est moi la notification";
// on definit egalement son texte qui le caracterise
String texteNotification = "Je suis une belle notifiacation ........";
// On configure notre notification avec tous les paramettres que l'on vient de creer
notification.setLatestEventInfo(this,titreNotification,texteNotification,pendingIntent);
notification.vibrate = new long[]{0, 200, 100, 200, 100, 200};
//Enfin on ajoute notre notification et son ID à notre gestionnaire de notification
notificationManager.notify(1990, notification);
} |
Partager