Android push Service Notification

在APP中經常會用到通知。這是個比較普遍的功能。比如網易新聞客戶端,有什么重大新聞的話會在通知欄彈出一條通知。

  在做程序過程中我也遇到這個需求。每隔7天就自動彈出通知,提醒用戶。在網上搜了搜,用了2天時間實現了。實現過程如下:

  一:通知要調用鬧鐘功能來實現,第一步設置鬧鐘

  /*參數1:context 參數2:喚醒的時間(毫秒格式)

   *功能:發出鬧鐘廣播

 

 public static void setAlarmTime(Context context, long timeInMillis) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("android.alarm.demo.action");
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = 7*24*60*60*1000; //7天時間的毫秒形式
    am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, 

    interval, sender);//參數2表示喚醒時間,參數2表示時間間隔。

  }


  要在需要的地方調用該方法,調用之后將調用時的時間存儲起來,為什么,下面會說。

  存儲代碼:

  SharedPreferences alarmTime = context.getSharedPreferences(Constant.ALARM_TIME, 0);
  Editor editor = alarmTime.edit();
  editor.putLong("theFirstNotifyBeginTime",System.currentTimeMillis());
  editor.commit();

  二:接收步驟一中發出的BroadCast

  

public class AlarmReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
  if ("android.alarm.demo.action".equals(intent.getAction())){

  //就在這里調用通知的操作
    NotifyUtil.addAppNotify(context); //我自己的通知的函數,見第三步
    SharedPreferences alarmTime = context.getSharedPreferences("alarm_time", 0);//為什么會有這個,接著往下看就行。
    Editor editor = alarmTime.edit();
    editor.putLong("lastNotifyTime",System.currentTimeMillis());
    editor.commit();
    return;
  }
}

}


  三:步驟二中要調用的通知函數。

public class NotifyUtil {

    public static void addAppNotify(Context context){
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(); 
    notification.icon = R.drawable.icon_app_notify; //通知圖標
    notification.tickerText = context.getResources().getString(R.string.app_tickerText); //通知的內容
    notification.defaults=Notification.DEFAULT_SOUND; //通知的鈴聲
    notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent(); 
    intent.setComponent(new ComponentName("carman.execise","carman.execise.Main"));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
    // 點擊狀態欄的圖標出現的提示信息設置 
    notification.setLatestEventInfo(context,context.getResources().getString(R.string.app_notify_title),
    context.getResources().getString(R.string.app_notify), pendingIntent);
    manager.notify(1, notification); //這個函數中第一個參數代表identifier.如果要同時彈出多條通知,每個通知的這個參數必須不同。

                   否則,后面的會覆蓋前面的通知。
}
}

  

  以上步驟就可以實現通知功能,但是還缺一個重啟的監聽。否則,手機一重啟所設置的鬧鐘就失效了。

  四:重啟恢復鬧鐘,通過一個廣播來實現

  因為重啟之后所設置的鬧鐘就失效了,必須通過該廣播重新計算好下次響的時間nextNotifyTime,并再次調用步驟一的方法。

 

 public class BootReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
    String action = intent.getAction();
    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
    SharedPreferences alarmTime = context.getSharedPreferences("alarm_time", 0);  //存儲數據的首選項
    long lastNotifyTime = alarmTime.getLong("lastNotifyTime",0);  //取得上次鬧鐘響的時間,也就是步驟二中存儲的值
    long nextNotifyTime;
    if(lastNotifyTime == 0){
    long theFirstNotifyBeginTime = alarmTime.getLong("theFirstNotifyBeginTime",0); //若沒彈出過通知,取得步驟一中存儲的值
    nextNotifyTime = 7*24*60*60*1000 + theFirstNotifyBeginTime;
    }else{
    nextNotifyTime = 7*24*60*60*1000 + lastNotifyTime;
    }
    if(nextNotifyTime <= System.currentTimeMillis()){
    NotifyUtil.setAlarmTime(context,System.currentTimeMillis());
    }else{
    NotifyUtil.setAlarmTime(context,nextNotifyTime); //再次設置為鬧鐘
    }
  }
  }
  }


  五:這些做好了之后調試還是通不過,因為需要在androidManifest中注冊下廣播的接收器才行。如下:

  步驟二的接收器,監聽鬧鐘

<receiver android:name=".notifications.AlarmReceiver">
    <intent-filter>
      <action android:name="android.alarm.demo.action" />
  </intent-filter>
  </receiver>


    步驟四的接收器,監聽重啟

<receiver android:name=".notifications.BootReceiver">
    <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
  </receiver>

  

  黃色字體部分為鬧鐘重啟監聽的相關代碼。

 本文由用戶 mark0614 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!