一迁客、創(chuàng)建一個普通Notification
- 創(chuàng)建通知構(gòu)造者
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
- 設(shè)置通知的圖標(biāo)穴肘、標(biāo)題和內(nèi)容文本
builder.setSmallIcon(R.mipmap.ic_laucher)
.setContentTitle("親拜马,福利到啦星持!")
.setContentText("點擊訪問購物頁面");
- 設(shè)置表示通知訪問的目標(biāo)組件的意圖
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
- 創(chuàng)建啟動Activity的PendingIntent
PendingIntent pi=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
- 設(shè)置通知的意圖
builder.setContentIntent(pi);
- 創(chuàng)建通知管理器
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- 創(chuàng)建Notifiaction實例
Notification notification=builder.build();
- 設(shè)置通知的標(biāo)志坛吁,點擊通知后寇损,通知自動清除
notification.flags=Notification.FLAG_AUTO_CANCEL;
- 發(fā)送通知
manager.notify(99,builder.build());
二藻茂、用通知Notification開啟Activity
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setContentTitle("親驹暑,購物啦")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("點擊我進(jìn)入購物界面");
Intent intent=new Intent(MainActivity.this,BuyActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//創(chuàng)建通知對象
Notification notification=builder.build();
notification.flags=Notification.FLAG_AUTO_CANCEL;
//獲取系統(tǒng)服務(wù),創(chuàng)建通知管理器
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(99,notification);
三辨赐、用Notification發(fā)送廣播
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
builder.setContentTitle("發(fā)送服務(wù)")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("點擊我發(fā)送服務(wù)");
Intent intent=new Intent(MainActivity.this,MyService.class);
PendingIntent pendingIntent=PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//創(chuàng)建通知對象
Notification notification=builder.build();
notification.flags=Notification.FLAG_AUTO_CANCEL;
//獲取系統(tǒng)服務(wù)优俘,創(chuàng)建通知管理器
NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(90,notification);