Here vous pouvez trouver une meilleure explication avec les codes sources sur les notifications.
La notification peut être une réaction à un événement. Par exemple, vous pouvez développer une application simple avec un seul bouton. Lorsque vous appuyez sur ce bouton, une notification s'affiche dans la barre d'état.
À propos du développement. Vous devez installer Android SDK, créer un émulateur de l'appareil. Aussi, il est très utile d'installer Android ADT - c'est un plugin pour Eclipse pour aider à développer des applications Android. Après cela, lorsque vous construisez une application, elle sera automatiquement installée sur l'émulateur.
Voici le code comment faire une simple notification:
package your.package
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AcNotificationTestMain extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final int SEND_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mBtnSend = (Button) findViewById(R.id.button1);
mBtnSend.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
Log.v("","OnClick...");
// Create an object of Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int icon = android.R.drawable.sym_action_email; // icon from resources
CharSequence tickerText = "New Notification"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Click me!"; // expanded message text
Intent notificationIntent = new Intent(this, AcNotificationTestMain.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(SEND_ID, notification);
}
}
et le fichier de mise en page:
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/>
<Button android:id="@+id/button1" android:text="@string/AcNotificationTest_BtnSendNotificationText" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Merci pour l'info - Je comprends tout à fait encore ne comment j'envoyer une notification à un utilisateur qui a installé l'application? Devons-nous envoyer une version mise à jour de l'application chaque fois que nous souhaitons envoyer une nouvelle notification? – Dancer
Pouvez-vous expliquer ce que vous voulez faire? – Yury
aussi - J'ai installé l'android sdk et l'émulateur. Je ne sais pas où je place le code mentionné dans le tutoriel. puis-je créer un nouveau fichier Java ou tout cela va dans le manifeste? désolé, c'est probablement très simple ... Une fois que ce service est configuré dans l'application - comment envoyer un nouveau message? Merci beaucoup pour votre aide en passant! – Dancer