Quellcode durchsuchen

Initial support for iOS

Sebastián Katzer vor 12 Jahren
Ursprung
Commit
bf9e5b8351
4 geänderte Dateien mit 246 neuen und 0 gelöschten Zeilen
  1. 37 0
      plugin.xml
  2. 33 0
      src/ios/CDVLocalNotification.h
  3. 111 0
      src/ios/CDVLocalNotification.m
  4. 65 0
      www/locale-notification.js

+ 37 - 0
plugin.xml

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
+        id="de.appplant.maintcall.cordova.plugin.local-notifications"
+        version="0.0.1">
+
+    <name>LocalNotifications</name>
+
+    <description>A bunch of local-notification plugins for Cordova 3.x.x</description>
+    <keywords>notification, localnotification, ios</keywords>
+    <license>GPL v2 License</license>
+
+    <author>Sebastián Katzer (github.com/katzer)</author>
+
+    <engines>
+        <engine name="cordova" version=">=3.0.0" />
+    </engines>
+
+    <!-- ios -->
+    <platform name="ios">
+
+        <config-file target="config.xml" parent="/*">
+		<feature name="LocalNotofication">
+			<param name="ios-package" value="CDVLocalNotification"/>
+		</feature>
+        </config-file>
+
+        <js-module src="www/local-notification.js" name="LocalNotification">
+            <clobbers target="plugin.notification.local" />
+        </js-module>
+
+	<header-file src="src/ios/CDVLocalNotification.h" />
+	<source-file src="src/ios/CDVLocalNotification.m" />
+
+    </platform>
+
+</plugin>

+ 33 - 0
src/ios/CDVLocalNotification.h

@@ -0,0 +1,33 @@
+/**
+ *  CDVLocalNotification.h
+ *  Cordova LocalNotification Plugin
+ *
+ *  Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
+ *  Copyright 2013 Sebastian Katzer. All rights reserved.
+ *  GPL v2 licensed
+ */
+
+#import <Foundation/Foundation.h>
+#import <Cordova/CDVPlugin.h>
+
+@interface LocalNotification : CDVPlugin {
+
+}
+
+// Fügt einen neuen Eintrag hinzu
+- (void) add:(CDVInvokedUrlCommand*)command;
+// Entfernt den anhand der ID angegebenen Eintrag
+- (void) cancel:(CDVInvokedUrlCommand*)command;
+// Entfernt alle registrierten Einträge
+- (void) cancelAll:(CDVInvokedUrlCommand*)command;
+
+@end
+
+
+@interface CDVPrinter (Private)
+
+- (NSMutableDictionary*) repeatDict;
+- (NSMutableDictionary*) userDict:(NSMutableDictionary*)options;
+- (UILocalNotification*) prepareNotification:(NSMutableDictionary*)options;
+
+@end

+ 111 - 0
src/ios/CDVLocalNotification.m

@@ -0,0 +1,111 @@
+/**
+ *  CDVLocalNotification.h
+ *  Cordova LocalNotification Plugin
+ *
+ *  Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
+ *  Copyright 2013 Sebastian Katzer. All rights reserved.
+ *  GPL v2 licensed
+ */
+
+ #import "LocalNotification.h"
+
+
+@implementation LocalNotification
+
+/**
+ * Fügt eine neue Notification-Eintrag hinzu.
+ *
+ * @param {NSMutableDictionary} options
+ */
+- (void) addNotification:(CDVInvokedUrlCommand*)command {
+	NSArray             *arguments    = [command arguments];
+	NSMutableDictionary *options      = [arguments objectAtIndex:0];
+	UILocalNotification *notification = [this prepareNotification:options];
+
+    notification.userInfo = [this userDict:options];
+
+	[[UIApplication sharedApplication] scheduleLocalNotification:notification];
+}
+
+/**
+ * Entfernt den anhand der ID angegebenen Eintrag.
+ *
+ * @param {NSString} id Die ID der Notification
+ */
+- (void) cancel:(CDVInvokedUrlCommand*)command {
+	NSArray  *arguments      = [command arguments];
+	NSString *notificationId = [arguments objectAtIndex:0];
+	NSArray  *notifications  = [[UIApplication sharedApplication] scheduledLocalNotifications];
+
+	for (UILocalNotification *notification in notifications) {
+		NSString *notId = [notification.userInfo objectForKey:@"notificationId"];
+
+		if ([notificationId isEqualToString:notId]) {
+			[[UIApplication sharedApplication] cancelLocalNotification:notification];
+		}
+	}
+}
+
+/**
+ * Entfernt alle registrierten Einträge.
+ */
+- (void) cancelAll:(CDVInvokedUrlCommand*)command {
+	[[UIApplication sharedApplication] cancelAllLocalNotifications];
+}
+
+/**
+ * @private
+ */
+- (NSMutableDictionary*) repeatDict {
+	NSMutableDictionary *repeatDict = [[NSMutableDictionary alloc] init];
+
+    [repeatDict setObject:[NSNumber numberWithInt:NSDayCalendarUnit]   forKey:@"daily"];
+    [repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit]  forKey:@"weekly"];
+    [repeatDict setObject:[NSNumber numberWithInt:NSMonthCalendarUnit] forKey:@"monthly"];
+    [repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit]  forKey:@"yearly"];
+    [repeatDict setObject:[NSNumber numberWithInt:0]                   forKey:@""];
+
+    return repeatDict;
+}
+
+/**
+ * @private
+ */
+- (NSMutableDictionary*) userDict:(NSMutableDictionary*)options {
+	NSString     *notificationId = [options objectForKey:@"id"];
+    NSString     *bg             = [options objectForKey:@"background"];
+    NSString     *fg             = [options objectForKey:@"foreground"];
+
+	NSDictionary *userDict       = [NSDictionary dictionaryWithObjectsAndKeys:notificationId, @"notificationId", bg, @"background", fg, @"foreground", nil];
+
+	return userDict;
+}
+
+/**
+ * @private
+ */
+- (UILocalNotification*) prepareNotification:(NSMutableDictionary*)options {
+	double               timestamp          = [[options objectForKey:@"date"] doubleValue];
+	NSString*            msg                = [options objectForKey:@"message"];
+	NSString*            action             = [options objectForKey:@"action"];
+    NSString*            sound              = [options objectForKey:@"sound"];
+    NSString*            repeat             = [options objectForKey:@"repeat"];
+	NSInteger            badge              = [[options objectForKey:@"badge"] intValue];
+	bool                 hasAction          = ([[options objectForKey:@"hasAction"] intValue] == 1) ? YES : NO;
+
+	NSDate*              date               = [NSDate dateWithTimeIntervalSince1970:timestamp];
+	UILocalNotification* notification       = [[UILocalNotification alloc] init];
+
+	notification.fireDate                   = date;
+	notification.hasAction                  = hasAction;
+	notification.timeZone                   = [NSTimeZone defaultTimeZone];
+    notification.repeatInterval             = [[[this repeatDict] objectForKey: repeat] intValue];
+	notification.alertBody                  = ([msg isEqualToString:@""])?nil:msg;
+	notification.alertAction                = action;
+    notification.soundName                  = sound;
+    notification.applicationIconBadgeNumber = badge;
+
+    return notification;
+}
+
+@end

+ 65 - 0
www/locale-notification.js

@@ -0,0 +1,65 @@
+/**
+ *  locale-notification.js
+ *  Cordova LocalNotification Plugin
+ *
+ *  Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
+ *  Copyright 2013 Sebastian Katzer. All rights reserved.
+ *  GPL v2 licensed
+ */
+
+var LocalNotification = function () {
+
+};
+
+LocalNotification.prototype = {
+    /**
+     * Fügt einen neuen Eintrag zur Registry hinzu.
+     *
+     * @param {Object} options
+     */
+    add: function (options) {
+        var defaults = {
+            date:      false,
+            message:   '',
+            hasAction: true,
+            action:    'View',
+            badge:     0,
+            id:        0,
+            sound:     '',
+            background:'',
+            foreground:''
+        };
+
+        for (var key in defaults) {
+            if (options[key] !== undefined) {
+                defaults[key] = options[key];
+            }
+        }
+
+        if (typeof defaults.date == 'object') {
+            defaults.date = Math.round(defaults.date.getTime()/1000);
+        }
+
+        cordova.exec(null, null, 'LocalNotification','add', [defaults]);
+    },
+
+    /**
+     * Entfernt die angegebene Notification.
+     *
+     * @param {String} id
+     */
+    cancel: function (id) {
+        cordova.exec(null, null, 'LocalNotification', 'cancel', [id]);
+    },
+
+    /**
+     * Entfernt alle registrierten Notifications.
+     */
+    cancelAll: function () {
+        cordova.exec(null, null, 'LocalNotification', 'cancelAll', []);
+    }
+};
+
+var plugin = new LocalNotification();
+
+module.exports = plugin;