Android發送多個notification

來源:互聯網
上載者:User

Android發送多個notification



//Android發送多個notification  ,PendingIntent的ID很重要。
public void addNotification(JSONArray args, CallbackContext callbackContext) throws JSONException {//NOTIFICATION_ID = args.getInt(6);NOTIFICATION_ID = (int)(Math.random()*10000);try {nm = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);Intent intent = new Intent(cx, cordova.getActivity().getClass());intent.putExtra("clickAction", args.getString(4));String clickActionParams = args.getJSONObject(5).toString();intent.putExtra("clickActionParams", clickActionParams);PendingIntent pIntent = PendingIntent.getActivity(cx, NOTIFICATION_ID, intent,PendingIntent.FLAG_UPDATE_CURRENT);int version = android.os.Build.VERSION.SDK_INT;Notification notify;// 如果版本號碼大於15.即採用notification.builder方法,如果版本號碼小於15,即採用舊方法,避免類似盧峰手機的問題if (version > 15) {notify = new Notification.Builder(cx)// 設定開啟該通知,該通知自動消失.setAutoCancel(true)// 設定顯示在狀態列的通知提示資訊.setTicker(args.getString(0))// 設定通知的表徵圖.setSmallIcon(R.drawable.icon)// 設定通知內容的標題.setContentTitle(args.getString(0))// 設定通知內容.setContentText(args.getString(1) + NOTIFICATION_ID).setWhen(System.currentTimeMillis())// 設改通知將要啟動程式的Intent.setContentIntent(pIntent).build();} else {notify = new Notification(R.drawable.icon, args.getString(0),System.currentTimeMillis());notify.setLatestEventInfo(cx, args.getString(0),args.getString(1), pIntent);}// 發送通知nm.notify(NOTIFICATION_ID, notify);// 通知顯示X秒後自動清除if (args.getBoolean(2)) {disappearTime = args.getLong(3);Handler handler = new Handler();handler.postDelayed(new Runnable() {public void run() {// TODO Auto-generated method stubnm.cancel(NOTIFICATION_ID);}}, disappearTime);}} catch (JSONException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.