@Override public void onReceive(Context context, Intent intent) { Options options = null; Bundle bundle = intent.getExtras(); JSONObject args; try { args = new JSONObject(bundle.getString(OPTIONS)); options = new Options(context).parse(args); } catch (JSONException e) { return; } this.context = context; this.options = options; // The context may got lost if the app was not running before LocalNotification.setContext(context); fireTriggerEvent(); if (options.getInterval() == 0) { LocalNotification.unpersist(options.getId()); } else if (isFirstAlarmInFuture()) { return; } else { LocalNotification.add(options.moveDate(), false); } Builder notification = buildNotification(); showNotification(notification); }
/** Ruft die `foreground` Callback Funktion auf. */ private void invokeForegroundCallback() { String function = options.getForeground(); // after reboot, LocalNotification.webView is always null // may be call foreground callback later if (function != null && LocalNotification.webView != null) { LocalNotification.webView.sendJavascript(function + "(" + options.getId() + ")"); } }
/** * Cancel the local notification. * * <p>Create an intent that looks similar, to the one that was registered using schedule. Making * sure the notification id in the action is the same. Now we can search for such an intent using * the 'getService' method and cancel it. */ public void cancel() { Intent intent = new Intent(context, receiver).setAction(options.getId()); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); getAlarmMgr().cancel(pi); getNotMgr().cancel(options.getIdAsInt()); unpersist(); }
/** Remove the notification from the Android shared Preferences. */ private void unpersist() { SharedPreferences.Editor editor = getPrefs().edit(); editor.remove(options.getId()); if (Build.VERSION.SDK_INT < 9) { editor.commit(); } else { editor.apply(); } }
/** * Persist the information of this notification to the Android Shared Preferences. This will allow * the application to restore the notification upon device reboot, app restart, retrieve * notifications, aso. */ private void persist() { SharedPreferences.Editor editor = getPrefs().edit(); editor.putString(options.getId(), options.toString()); if (Build.VERSION.SDK_INT < 9) { editor.commit(); } else { editor.apply(); } }
/** * Set intent to handle the delete event. Will clean up some persisted preferences. * * @param builder Local notification builder instance */ private void applyDeleteReceiver(NotificationCompat.Builder builder) { if (clearReceiver == null) return; Intent deleteIntent = new Intent(context, clearReceiver) .setAction(options.getId()) .putExtra(Options.EXTRA, options.toString()); PendingIntent dpi = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(dpi); }
/** Schedule the local notification. */ public void schedule() { long triggerTime = getNextTriggerTime(); persist(); // Intent gets called when the Notification gets fired Intent intent = new Intent(context, receiver) .setAction(options.getId()) .putExtra(Options.EXTRA, options.toString()); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi); }
/** * Set an alarm. * * @param options The options that can be specified per alarm. */ public static void add(Options options) { long triggerTime = options.getDate(); Intent intent = new Intent(context, Receiver.class) .setAction("" + options.getId()) .putExtra(Receiver.OPTIONS, options.getJSONObject().toString()); AlarmManager am = getAlarmManager(); PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); if (options.getInterval() > 0) { am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getInterval(), pi); } else { am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi); } }
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { LocalNotification.webView = super.webView; LocalNotification.context = super.cordova.getActivity().getApplicationContext(); if (action.equalsIgnoreCase("add")) { JSONObject arguments = args.optJSONObject(0); final Options options = new Options(context).parse(arguments); persist(options.getId(), args); cordova .getThreadPool() .execute( new Runnable() { public void run() { add(options); } }); return true; } if (action.equalsIgnoreCase("cancel")) { String id = args.optString(0); cancel(id); unpersist(id); return true; } if (action.equalsIgnoreCase("cancelAll")) { cancelAll(); unpersistAll(); return true; } // Returning false results in a "MethodNotFound" error. return false; }
/** Shows the notification */ @SuppressWarnings("deprecation") @SuppressLint("NewApi") private void showNotification(Builder notification) { NotificationManager mgr = (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 mgr.notify(id, notification.getNotification()); } else if (Build.VERSION.SDK_INT > 15) { // Notification for Jellybean and above mgr.notify(id, notification.build()); } }
/** Fires ontrigger event. */ private void fireTriggerEvent() { LocalNotification.fireEvent("trigger", options.getId(), options.getJSON()); }