Sebastián Katzer 12 лет назад
Родитель
Сommit
b539019b5d
1 измененных файлов с 75 добавлено и 33 удалено
  1. 75 33
      src/android/LocalNotificationReceiver.java

+ 75 - 33
src/android/LocalNotificationReceiver.java

@@ -14,7 +14,7 @@ import java.util.Calendar;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import android.annotation.TargetApi;
+import android.annotation.SuppressLint;
 import android.app.ActivityManager;
 import android.app.Notification;
 import android.app.Notification.Builder;
@@ -32,17 +32,17 @@ import android.os.Bundle;
  * Android notification bar. The notification uses the default notification
  * sound and it vibrates the phone.
  */
-@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
 public class LocalNotificationReceiver extends BroadcastReceiver {
 
-	public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
+	public final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
+
+	private Context context;
+	private LocalNotificationOptions options;
 
 	@Override
 	public void onReceive (Context context, Intent intent) {
-		NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 		LocalNotificationOptions options    = null;
 		Bundle bundle                       = intent.getExtras();
-		int id                              = 0;
 		JSONObject args;
 
 		try {
@@ -50,17 +50,33 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 			options = new LocalNotificationOptions(args);
 		} catch (JSONException e) {}
 
-		try {
-			id = Integer.parseInt(options.getId());
-		} catch (Exception e) {}
+		this.context = context;
+		this.options = options;
+
+		if (options.getInterval() == 0) {
+			LocalNotification.unpersist(options.getId());
+		} else if (isFirstAlarmInFuture()) {
+			return;
+		}
+
+		Builder notification = buildNotification();
+
+		if (!isInBackground(context)) {
+			// exec foreground callback
+			invokeForegroundCallback(options);
+		}
+
+		showNotification(notification);
+	}
 
-		/*
-		 * If you set a repeating alarm at 11:00 in the morning and it
-		 * should trigger every morning at 08:00 o'clock, it will
-		 * immediately fire. E.g. Android tries to make up for the
-		 * 'forgotten' reminder for that day. Therefore we ignore the event
-		 * if Android tries to 'catch up'.
-		 */
+	/*
+	 * If you set a repeating alarm at 11:00 in the morning and it
+	 * should trigger every morning at 08:00 o'clock, it will
+	 * immediately fire. E.g. Android tries to make up for the
+	 * 'forgotten' reminder for that day. Therefore we ignore the event
+	 * if Android tries to 'catch up'.
+	 */
+	private Boolean isFirstAlarmInFuture () {
 		if (options.getInterval() > 0) {
 			Calendar now    = Calendar.getInstance();
 			Calendar alarm  = options.getCalendar();
@@ -71,21 +87,21 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 			int currentMin  = now.get(Calendar.MINUTE);
 
 			if (currentHour != alarmHour && currentMin != alarmMin) {
-				return;
+				return true;
 			}
-		} else {
-			LocalNotification.unpersist(options.getId());
-		};
+		}
 
-		String packageName          = context.getPackageName();
-		Intent launchIntent         = context.getPackageManager().getLaunchIntentForPackage(packageName);
-		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+		return false;
+	}
 
+	/**
+	 * Erstellt die Notification.
+	 */
+	private Builder buildNotification () {
 		Builder notification = new Notification.Builder(context)
 		.setContentTitle(options.getTitle())
 		.setContentText(options.getSubTitle())
 		.setNumber(options.getBadge())
-		.setContentIntent(contentIntent)
 		.setTicker(options.getTitle())
 		.setSmallIcon(options.getIcon());
 
@@ -93,22 +109,48 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 			if (isInBackground(context)) {
 				// app is in background
 				notification.setDefaults(Notification.DEFAULT_SOUND);
-			} else {
-				// exec foreground callback
-				invokeForegroundCallback(options);
 			}
 		} catch (Exception e) {
 			// missing GET_TASKS permission
 			notification.setDefaults(Notification.DEFAULT_SOUND);
 		}
 
-		/*
-		 * If you want all reminders to stay in the notification bar, you should
-		 * generate a random ID. If you want do replace an existing
-		 * notification, make sure the ID below matches the ID that you store in
-		 * the alarm intent.
-		 */
-		notificationMgr.notify(id, notification.build());
+		setClickEvent(notification);
+
+		return notification;
+	}
+
+	/**
+	 * Fügt der Notification einen onclick Handler hinzu.
+	 */
+	private Builder setClickEvent (Builder notification) {
+		String packageName          = context.getPackageName();
+		Intent launchIntent         = context.getPackageManager().getLaunchIntentForPackage(packageName);
+		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+
+		return notification.setContentIntent(contentIntent);
+	}
+
+	/**
+	 * Zeigt die Notification an.
+	 */
+	@SuppressWarnings("deprecation")
+	@SuppressLint("NewApi")
+	private void showNotification (Builder notification) {
+		NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+		int id                              = 0;
+
+		try {
+			id = Integer.parseInt(options.getId());
+		} catch (Exception e) {}
+
+        if (Build.VERSION.SDK_INT<16) {
+            // build notification for HoneyComb to ICS
+            notificationMgr.notify(id, notification.getNotification());
+        } else if (Build.VERSION.SDK_INT>15) {
+            // Notification for Jellybean and above
+        	notificationMgr.notify(id, notification.build());
+        }
 	}
 
 	/**