android當中的訊息推送
阿新 • • 發佈:2019-02-08
作為訊息推送,一般是捆綁service進行間歇性的請求伺服器獲得資料,以新訊息提醒的方式進行的。
這裡只放出訊息推送的程式碼。
以下為程式碼:
//更新通知欄 /*獲取狀態通知欄管理*/ NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); /*例項化通知欄構造器NotificationCompat.Builder*/ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getBaseContext()); /*點選事件*/ Intent intent = new Intent(getBaseContext(),OrderDetailActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, 0); /*配置通知欄*/ mBuilder.setContentTitle("你有新的訊息")//設定通知欄標題 .setContentText("測試內容"+getServerMessage()) //設定通知欄顯示內容 // .setContentIntent(pendingIntent)//通知欄點選事件 // .setNumber(number) //設定通知集合的數量 .setTicker("通知來啦") //通知首次出現在通知欄,帶上升動畫效果的 .setWhen(System.currentTimeMillis())//通知產生的時間,會在通知資訊裡顯示,一般是系統獲取到的時間 .setPriority(Notification.PRIORITY_DEFAULT) //設定該通知優先順序 // .setAutoCancel(true)//設定這個標誌當用戶單擊面板就可以讓通知將自動取消 .setOngoing(false)//ture,設定他為一個正在進行的通知。他們通常是用來表示一個後臺任務,使用者積極參與(如播放音樂)或以某種方式正在等待,因此佔用裝置(如一個檔案下載,同步操作,主動網路連線) .setDefaults(Notification.DEFAULT_VIBRATE)//向通知新增聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的使用者預設設定,使用defaults屬性,可以組合 //Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 新增聲音 // requires VIBRATE permission .setSmallIcon(R.drawable.ic_launcher);//設定通知小ICON mNotificationManager.notify(i, mBuilder.build()); i++;//作為訊息推送的id