Skip to content

Commit 5c2577b

Browse files
Cleaned up iOS impl.
1 parent ea93ffa commit 5c2577b

File tree

8 files changed

+49
-399
lines changed

8 files changed

+49
-399
lines changed

local-notifications.ios.js

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
var LocalNotifications = require("./local-notifications-common");
2-
var application = require("application"); // TODO unused
32

43
LocalNotifications._addObserver = function (eventName, callback) {
54
return NSNotificationCenter.defaultCenter().addObserverForNameObjectQueueUsingBlock(eventName, null, NSOperationQueue.mainQueue(), callback);
65
};
76

8-
var pushHandler,
9-
pushManager;
10-
11-
var pendingReceivedNotifications = [];
12-
var receivedNotificationCallback = null;
7+
var pendingReceivedNotifications = [],
8+
notificationHandler,
9+
notificationManager,
10+
receivedNotificationCallback = null;
1311

1412
(function () {
1513
// grab 'em here, store em in JS, and give them to the callback when addOnMessageReceivedCallback is wired
1614
LocalNotifications.notificationReceivedObserver = LocalNotifications._addObserver("notificationReceived", function (result) {
17-
var notificationDetails = result.userInfo.objectForKey('message');
15+
var notificationDetails = JSON.parse(result.userInfo.objectForKey('message'));
1816
console.log("------- notificationReceivedObserver: " + notificationDetails);
1917
if (receivedNotificationCallback != null) {
2018
receivedNotificationCallback(notificationDetails);
@@ -23,8 +21,8 @@ var receivedNotificationCallback = null;
2321
}
2422
});
2523

26-
pushHandler = Notification.alloc().init();
27-
pushManager = NotificationManager.alloc().init();
24+
notificationHandler = Notification.alloc().init();
25+
notificationManager = NotificationManager.alloc().init();
2826
})();
2927

3028
LocalNotifications.addOnMessageReceivedCallback = function (callback) {
@@ -36,23 +34,6 @@ LocalNotifications.addOnMessageReceivedCallback = function (callback) {
3634
}
3735
pendingReceivedNotifications = [];
3836

39-
40-
// TODO do we need this for permission stuff?
41-
LocalNotifications.didRegisterUserNotificationSettingsObserver = LocalNotifications._addObserver("didRegisterUserNotificationSettings", function (result) {
42-
//NSNotificationCenter.defaultCenter().removeObserver(LocalNotifications.notificationReceivedObserver);
43-
//LocalNotifications.notificationReceivedObserver = undefined;
44-
//var notificationDetails = result.userInfo.objectForKey('message');
45-
console.log("------- received didRegisterUserNotificationSettings in JS: " + result);
46-
// invoke the callback here
47-
//success(token);
48-
callback(result);
49-
});
50-
51-
//setTimeout(function() {
52-
// console.log("---- checking pending notification");
53-
// pushHandler.checkPendingNotification();
54-
//}, 500);
55-
5637
resolve(true);
5738
} catch (ex) {
5839
console.log("Error in LocalNotifications.addOnMessageReceivedCallback: " + ex);
@@ -81,15 +62,25 @@ LocalNotifications._hasPermission = function () {
8162
LocalNotifications.requestPermission = function (arg) {
8263
return new Promise(function (resolve, reject) {
8364
try {
84-
resolve(LocalNotifications._requestPermission());
65+
LocalNotifications._requestPermission(function(granted) {
66+
resolve(granted);
67+
})
8568
} catch (ex) {
8669
console.log("Error in LocalNotifications.requestPermission: " + ex);
8770
reject(ex);
8871
}
8972
});
9073
};
9174

92-
LocalNotifications._requestPermission = function () {
75+
LocalNotifications._requestPermission = function (callback) {
76+
77+
LocalNotifications.didRegisterUserNotificationSettingsObserver = LocalNotifications._addObserver("didRegisterUserNotificationSettings", function (result) {
78+
NSNotificationCenter.defaultCenter().removeObserver(LocalNotifications.didRegisterUserNotificationSettingsObserver);
79+
LocalNotifications.didRegisterUserNotificationSettingsObserver = undefined;
80+
var granted = result.userInfo.objectForKey('message');
81+
callback(granted != "false");
82+
});
83+
9384
var settings = UIApplication.sharedApplication().currentUserNotificationSettings();
9485
var types = settings.types | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
9586
settings = UIUserNotificationSettings.settingsForTypesCategories(types, null);
@@ -116,7 +107,7 @@ LocalNotifications._schedulePendingNotifications = function () {
116107
userInfoDict.setObjectForKey(options.body, "body");
117108
notification.userInfo = userInfoDict;
118109

119-
// TODO add these
110+
// TODO add these after v1
120111
// notification.repeatInterval = 1;
121112
// notification.soundName = null;
122113
// notification.resumeApplicationInBackground = true;
@@ -183,11 +174,13 @@ LocalNotifications.schedule = function (arg) {
183174
LocalNotifications.pendingNotifications = arg;
184175

185176
if (!LocalNotifications._hasPermission()) {
186-
LocalNotifications._requestPermission();
187-
reject("No permission yet, we asked it now");
177+
LocalNotifications._requestPermission(function() {
178+
LocalNotifications._schedulePendingNotifications();
179+
})
188180
} else {
189-
LocalNotifications._schedulePendingNotifications(); // TODO pass in (resolve, reject)
181+
LocalNotifications._schedulePendingNotifications();
190182
}
183+
191184
resolve();
192185
} catch (ex) {
193186
console.log("Error in LocalNotifications.schedule: " + ex);

native-src/ios/LocalNotificationsPlugin/LocalNotificationsPlugin.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ FOUNDATION_EXPORT double LocalNotificationsVersionNumber;
77
FOUNDATION_EXPORT const unsigned char LocalNotificationsPluginVersionString[];
88

99
// In this header, you should import all the public headers of your framework
10-
1110
#import <Notification.h>
1211
#import <NotificationManager.h>

native-src/ios/LocalNotificationsPlugin/Notification.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@interface Notification : NSObject <UIApplicationDelegate>
55
{
66
UILocalNotification *notificationMessage;
7-
BOOL isInline;
7+
BOOL isInline;
88
}
99

1010
@property (nonatomic, strong) UILocalNotification *notificationMessage;
@@ -13,15 +13,12 @@
1313

1414
+ (instancetype)sharedInstance;
1515

16-
-(void)register:(NSMutableDictionary *)options;
17-
-(void)registerUserNotificationSettings:(NSDictionary*)options;
16+
//-(void)register:(NSMutableDictionary *)options;
17+
//-(void)registerUserNotificationSettings:(NSDictionary*)options;
1818
-(void)setApplicationIconBadgeNumber:(NSMutableDictionary *)options;
1919
-(void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;
20-
//-(void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
21-
//-(void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
2220
-(void)notificationReceived;
2321
-(void)success:(NSString *)eventName WithMessage:(NSString *)message;
2422
-(void)success:(NSString *)eventName WithDictionary:(NSMutableDictionary *)userInfo;
2523
-(void)fail:(NSString *)eventName WithMessage:(NSString *)message withError:(NSError *)error;
26-
-(void)checkPendingNotification;
2724
@end

native-src/ios/LocalNotificationsPlugin/Notification.m

Lines changed: 12 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
const NSString * badgeKey = @"badge";
77
const NSString * soundKey = @"sound";
88
const NSString * alertKey = @"alert";
9-
//const NSString * didRegisterEventName = @"didRegisterForRemoteNotificationsWithDeviceToken";
10-
const NSString * didRegisterUserNotificationsEventName = @"didRegisterUserNotificationSettings2";
11-
//const NSString * didFailToRegisterEventName = @"didFailToRegisterForRemoteNotificationsWithError";
9+
static NSString * didRegisterUserNotificationsEventName = @"didRegisterUserNotificationSettings";
1210
const NSString * notificationReceivedEventName = @"notificationReceived";
1311
const NSString * setBadgeNumberEventName = @"setApplicationIconBadgeNumber";
14-
// HUH!? Kan deze plugin ook al local notifications? Test in een project met de {N} push plugin!!
15-
// -- ofwel, start een nieuw project met deze plugin en kijk wat ie doet
1612
const NSString * didRegisterUserNotificationSettingsEventName = @"didRegisterUserNotificationSettings";
1713
const NSString * failToRegisterUserNotificationSettingsEventName = @"failToRegisterUserNotificationSettings";
1814

@@ -33,6 +29,7 @@ + (instancetype)sharedInstance
3329
return sharedInstance;
3430
}
3531

32+
/*
3633
-(void)register:(NSMutableDictionary *)options
3734
{
3835
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
@@ -71,6 +68,7 @@ -(void)register:(NSMutableDictionary *)options
7168
7269
[self notificationReceived];
7370
}
71+
*/
7472

7573
- (BOOL)isTrue:(NSString *)key fromOptions:(NSMutableDictionary *)options
7674
{
@@ -83,114 +81,13 @@ - (BOOL)isTrue:(NSString *)key fromOptions:(NSMutableDictionary *)options
8381
return false;
8482
}
8583

86-
- (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
84+
- (void)didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings
8785
{
88-
NSLog(@"------ didRegisterUserNotificationSettings");
89-
90-
91-
92-
NSMutableDictionary *results = [NSMutableDictionary dictionary];
93-
94-
//#if !TARGET_IPHONE_SIMULATOR
95-
// Get Bundle Info for Remote Registration (handy if you have more than one app)
96-
[results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"];
97-
[results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"];
98-
99-
/*
100-
// Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them.
101-
NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
102-
103-
// Set the defaults to disabled unless we find otherwise...
104-
NSString *pushBadge = @"disabled";
105-
NSString *pushAlert = @"disabled";
106-
NSString *pushSound = @"disabled";
107-
108-
// Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which
109-
// one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the
110-
// single notification types will only match if they are the ONLY one enabled. Likewise, when we are checking for a pair of notifications, it will only be
111-
// true if those two notifications are on. This is why the code is written this way
112-
if(rntypes & UIRemoteNotificationTypeBadge){
113-
pushBadge = @"enabled";
114-
}
115-
if(rntypes & UIRemoteNotificationTypeAlert) {
116-
pushAlert = @"enabled";
117-
}
118-
if(rntypes & UIRemoteNotificationTypeSound) {
119-
pushSound = @"enabled";
120-
}
121-
122-
[results setValue:pushBadge forKey:@"pushBadge"];
123-
[results setValue:pushAlert forKey:@"pushAlert"];
124-
[results setValue:pushSound forKey:@"pushSound"];
125-
126-
// Get the users Device Model, Display Name, Token & Version Number
127-
UIDevice *dev = [UIDevice currentDevice];
128-
[results setValue:dev.name forKey:@"deviceName"];
129-
[results setValue:dev.model forKey:@"deviceModel"];
130-
[results setValue:dev.systemVersion forKey:@"deviceSystemVersion"];
131-
*/
132-
[self success:didRegisterUserNotificationsEventName WithMessage:[NSString stringWithFormat:@"%@", @"nice"]];
133-
//#endif
86+
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
87+
bool ok = settings.types & types;
88+
[self success:didRegisterUserNotificationsEventName WithMessage:[NSString stringWithFormat:@"%@", ok ? @"true" : @"false"]];
13489
}
13590

136-
137-
/*
138-
- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
139-
{
140-
NSMutableDictionary *results = [NSMutableDictionary dictionary];
141-
NSString *token = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""]
142-
stringByReplacingOccurrencesOfString:@">" withString:@""]
143-
stringByReplacingOccurrencesOfString: @" " withString: @""];
144-
[results setValue:token forKey:@"deviceToken"];
145-
146-
#if !TARGET_IPHONE_SIMULATOR
147-
// Get Bundle Info for Remote Registration (handy if you have more than one app)
148-
[results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"];
149-
[results setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"appVersion"];
150-
151-
// Check what Notifications the user has turned on. We registered for all three, but they may have manually disabled some or all of them.
152-
NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
153-
154-
// Set the defaults to disabled unless we find otherwise...
155-
NSString *pushBadge = @"disabled";
156-
NSString *pushAlert = @"disabled";
157-
NSString *pushSound = @"disabled";
158-
159-
// Check what Registered Types are turned on. This is a bit tricky since if two are enabled, and one is off, it will return a number 2... not telling you which
160-
// one is actually disabled. So we are literally checking to see if rnTypes matches what is turned on, instead of by number. The "tricky" part is that the
161-
// single notification types will only match if they are the ONLY one enabled. Likewise, when we are checking for a pair of notifications, it will only be
162-
// true if those two notifications are on. This is why the code is written this way
163-
if(rntypes & UIRemoteNotificationTypeBadge){
164-
pushBadge = @"enabled";
165-
}
166-
if(rntypes & UIRemoteNotificationTypeAlert) {
167-
pushAlert = @"enabled";
168-
}
169-
if(rntypes & UIRemoteNotificationTypeSound) {
170-
pushSound = @"enabled";
171-
}
172-
173-
[results setValue:pushBadge forKey:@"pushBadge"];
174-
[results setValue:pushAlert forKey:@"pushAlert"];
175-
[results setValue:pushSound forKey:@"pushSound"];
176-
177-
// Get the users Device Model, Display Name, Token & Version Number
178-
UIDevice *dev = [UIDevice currentDevice];
179-
[results setValue:dev.name forKey:@"deviceName"];
180-
[results setValue:dev.model forKey:@"deviceModel"];
181-
[results setValue:dev.systemVersion forKey:@"deviceSystemVersion"];
182-
183-
[self success:didRegisterEventName WithMessage:[NSString stringWithFormat:@"%@", token]];
184-
#endif
185-
}
186-
187-
- (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
188-
{
189-
[self fail:didFailToRegisterEventName WithMessage:@"" withError:error];
190-
}
191-
*/
192-
193-
// NOTE: to update the framework in the demo app, copy the built fwk to the /lib/iOS folder, then simply run the project
19491
- (void)notificationReceived
19592
{
19693
if (self.notificationMessage)
@@ -209,14 +106,9 @@ - (void)notificationReceived
209106
}
210107

211108
[jsonStr appendString:@"}"];
212-
NSLog(@"----- notificationReceived, has notificationMessage 3: %@", jsonStr);
213109
[self success:notificationReceivedEventName WithMessage:jsonStr];
214110
self.notificationMessage = nil;
215111
}
216-
if (self.launchNotification)
217-
{
218-
NSLog(@"----- notificationReceived, has launchNotification!");
219-
}
220112
}
221113

222114
-(void)parseDictionary:(NSDictionary *)inDictionary intoJSON:(NSMutableString *)jsonString
@@ -252,6 +144,7 @@ - (void)setApplicationIconBadgeNumber:(NSMutableDictionary *)options
252144
[self success:setBadgeNumberEventName WithMessage:[NSString stringWithFormat:@"app badge count set to %d", badge]];
253145
}
254146

147+
/*
255148
- (void)registerUserNotificationSettings:(NSDictionary*)options
256149
{
257150
NSLog(@"--- in registerUserNotificationSettings");
@@ -262,7 +155,6 @@ - (void)registerUserNotificationSettings:(NSDictionary*)options
262155
return;
263156
}
264157
265-
/*
266158
NSArray *categories = [options objectForKey:@"categories"];
267159
if (categories == nil) {
268160
[self fail:failToRegisterUserNotificationSettingsEventName WithMessage:@"No categories specified" withError:nil];
@@ -333,19 +225,19 @@ - (void)registerUserNotificationSettings:(NSDictionary*)options
333225
334226
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:nsTypes categories:nsCategorySet];
335227
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
336-
337-
*/
338-
228+
339229
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
340230
UIUserNotificationType types = settings.types|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound;
341231
settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
342232
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
343233
344234
#endif
345-
[self success:didRegisterUserNotificationSettingsEventName WithMessage:[NSString stringWithFormat:@"%@", @"user notifications registered!"]];
235+
[self success:didRegisterUserNotificationSettingsEventName
236+
WithMessage:[NSString stringWithFormat:@"%@", @"user notifications registered!"]];
346237
347238
// [self checkPendingNotification];
348239
}
240+
*/
349241

350242
/*
351243
-(void)checkPendingNotification {
@@ -430,13 +322,11 @@ - (NSMutableArray *)launchNotification
430322

431323
- (void)setLaunchNotification:(UILocalNotification *)notification
432324
{
433-
NSLog(@"---- setLaunchNotification");
434325
objc_setAssociatedObject(self, &launchNotificationKey, notification, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
435326
}
436327

437328
- (void)dealloc
438329
{
439-
NSLog(@"---- dealloc, nilling launchNotification");
440330
self.launchNotification = nil;
441331
}
442332

0 commit comments

Comments
 (0)