Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dealing with push notifications #12

Closed
cannyboy opened this issue Dec 12, 2013 · 3 comments
Closed

Dealing with push notifications #12

cannyboy opened this issue Dec 12, 2013 · 3 comments

Comments

@cannyboy
Copy link

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:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateActive)
    {
        [[MessageBarManager sharedInstance] showMessageWithUserInfo:userInfo
                                                              type:MessageBarMessageTypeInfo
        ];
    } else ...

... 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.

@terryworona
Copy link
Owner

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.

@cannyboy
Copy link
Author

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
    ];

@terryworona
Copy link
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants