Closed cannyboy closed 10 years ago
I'd rather keep the control mutually exclusive of any single use-case. Your code above is half-way there to having it absorb remote (or local) notifications; I don't really want to bake that into the current implementation.
Well, I think a message bar which can deal with push notifications would be pretty useful.
Here's a quick hack which i haven't thoroughly tested. It uses the "loc-args" & "loc-key" parameters of the userInfo dictionary. A better way would be to work out what kind of notification format the JSON is and deal with it appropriately. Examples are at the bottom of this page: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html (for instance, is the 'alert' key an array or string?)
- (void)showMessageWithTitle:(NSString*)title userInfo:(NSDictionary*)userInfo type:(MessageBarMessageType)type duration:(CGFloat)duration callback:(void (^)())callback
{
NSString *arg1 = @"";
NSString *arg2 = @"";
NSString *arg3 = @"";
NSString *arg4 = @"";
int i = 0;
for (NSString *arg in userInfo[@"aps"][@"alert"][@"loc-args"]) {
switch (i) {
case 0:
arg1 = arg;
break;
case 1:
arg2 = arg;
break;
case 2:
arg3 = arg;
break;
case 3:
arg4 = arg;
break;
default:
break;
}
i++;
}
NSString *localizedStringKey = userInfo[@"aps"][@"alert"][@"loc-key"];
NSString *description = [NSString stringWithFormat:NSLocalizedString(localizedStringKey, nil), arg1, arg2, arg3, arg4];
MessageView *messageView = [[MessageView alloc] initWithTitle:title description:description type:type];
messageView.callbacks = callback ? [NSArray arrayWithObject:callback] : [NSArray array];
messageView.hasCallback = callback ? YES : NO;
messageView.duration = duration;
messageView.hidden = YES;
[[[UIApplication sharedApplication] keyWindow] insertSubview:messageView atIndex:1];
[self.messageBarQueue addObject:messageView];
if (!self.messageVisible)
{
[self showNextMessage];
}
}
and it can be called with
[[MessageBarManager sharedInstance] showMessageWithTitle:@"Title" userInfo:userInfo
type:MessageBarMessageTypeInfo
];
Like I said, I want the control to remain decoupled from any single use case (ie. remote notifications, reachability, etc).
Your code looks great, and you should absolutely use it to support remote notification messages, I just don't think it belongs in the library.
I would recommend subclassing the manager or putting it into a category (ie. MessageBarManager+Notifications).
MessageBarManager seems ideal for dealing with push notifications when the user is actually running the app. Could MessageBarManager be made to deal with the push notification userInfo when the app is running? Something like:
... there is quite a lost of complexity that is handled with normal notifications ("loc-args", "loc-key" etc), so I'm guessing it might be difficult.