目錄
Notification
NotificationBuilder
Notification
private void showNotification() {
Notification notification=new Notification();//初始化notification
notification.icon= R.mipmap.ic_launcher;//設置通知的圖片
notification.tickerText="我是一個消息";//設置狀態欄的文本
notification.flags=Notification.FLAG_AUTO_CANCEL;//設置爲 可以取消
Intent intent=new Intent(getApplicationContext(),MainActivity.class);//設置pendingIntent事件
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);//設置pendingInten使用方式
notification.setLatestEventInfo(getApplicationContext(),"我是一個標題","我是內容",pend);
notification.when=System.currentTimeMillis();//設置顯示通知的時間
mNotificationManager.notify(1,notification); //將通知發送給id爲1的用戶
}
![這裏寫圖片描述](http://img.blog.csdn.net/20150830092248962)
NotificationBuilder
private void showNotificationBuilder() {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);//設置pendingIntent事件
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(),1, intent, PendingIntent.FLAG_ONE_SHOT);//設置pendingInten使用方式
Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("我是一條消息").setContentTitle("我是標題")
.setContentText("我是文本吧").setContentInfo("我是消息").setShowWhen(true).setAutoCancel(true).build();
mNotificationManager.notify(2,notification);
}