標籤:
notification簡單使用
1.不推薦 , 已不使用
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "This is ticker text", System.currentTimeMillis()); Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //被刪除 notification.setLatestEventInfo(this, "This is content title","This is content text", pi); manager.notify(1, notification);
2.系統推薦
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, DD.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(android.R.drawable.sym_def_app_icon) //表徵圖 .setContentTitle("標題") .setContentInfo("右下角") .setContentText("內容") .setAutoCancel(true) //點擊一下就消失 .setContentIntent(pendingIntent) //延遲意圖 .setTicker("剛出來是在手機最上方顯示,一會就消失") .setWhen(System.currentTimeMillis()) //訊息的時間 .build(); //建立notification// notification.flags = Notification.FLAG_AUTO_CANCEL; 跟setAutoCancel一樣作用 //顯示notification 第一個參數不能重複,否則就只能顯示重複的最後一個notification manager.notify(1,notification);
3. 自訂notification
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_main); remoteViews.setImageViewResource(R.id.imageView,android.R.drawable.sym_def_app_icon); remoteViews.setTextViewText(R.id.textText,"textview 的內容"); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(this, DD.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new NotificationCompat.Builder(this) .setAutoCancel(true) //點擊一下就消失 .setContentIntent(pendingIntent) //延遲意圖 .setContent(remoteViews) //設定自訂的view .build(); manager.notify(1,notification);
android notification 理解