1. 程式人生 > >Android實現本地推送通知的解決方案

Android實現本地推送通知的解決方案

                廢話不多說,直接進入正題. 一、自己寫程式碼建立通知,可以參考以下程式碼:   NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);   String title = "通知標題" ;   String content = "通知內容" ;   //1.例項化一個通知,指定圖示、概要、時間   Notification n=new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis()); 
  //2.指定通知的標題、內容和intent   Intent intent = new Intent(this, MainActivity.class);   PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);   n.setLatestEventInfo(this, title, content, pi);   //3.指定聲音   //n.defaults = Notification.DEFAULT_SOUND;   //4.傳送通知   nm.notify(1, n); 二、使用第三方.這種方式就簡單多了,而且功能更豐富,他們不僅僅是建立一條簡單的通知那麼簡單,還能設定使用者多少天沒開啟過應用就彈出該通知,只要整合SDK就可以了. 
下面以智遊推送為案例講講. 函式原型 PushManager.getInstance(). isOpenRemind(Context context , boolean state , String content , int days , int startTime , int endTime) ; 引數 context: Context物件 state : 設定是否開啟智慧提醒,true:表示開啟智慧提醒,false表示暫停智慧提醒 content:設定提醒內容 days:相隔幾天之後沒有開啟應用,進行提醒 startTime:在startTime之後進行提醒0-23 endTime:在endTime之前進行提醒0-23 
返回值 true:開啟智慧提醒成功 false:開啟智慧提醒失敗 示例 boolean result = PushManager.getInstance().isOpenRemind(this, true, "您已經3天沒開啟過微信了,有很多留言別錯過哦", 3, 0, 23) ; 就是這樣,一條推送通知就生成了,那麼,又會遇到一個問題,如果應用釋出出去,我想修改怎麼辦呢?沒關係,可以通過智遊推送後臺Portal動態設定,附個截圖就知道