Skip to content

Commit

Permalink
Redid local notifcations
Browse files Browse the repository at this point in the history
TWMessageBar is still used for in-app messages.

UILocalNotifications work for when we're outside the app.

Badge numbers work, but opening app from a local notification doesn't go to a specific window yet.

Still need to set default notifcation style to banners, and user can set it to alert views through Settings later.
  • Loading branch information
Jman012 committed Mar 26, 2014
1 parent c8703e1 commit 0dfcf92
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
7 changes: 4 additions & 3 deletions Toxicity/Friends/TXCFriendsListTableViewController.m
Expand Up @@ -149,9 +149,10 @@ - (void)friendListUpdate {
[self.tableView reloadData];
}

-(void) processingNewMessageWithNotificaton:(NSNotification*) notification {
self.lastMessage = [NSString stringWithFormat:@"%@", notification.userInfo[@"message"]];
self.numberOfLastMessageAuthor = [notification.userInfo[@"friendNumber"] integerValue];
- (void)processingNewMessageWithNotificaton:(NSNotification *)notification {
TXCMessageObject *tempMessage = (TXCMessageObject *)notification.object;
self.lastMessage = tempMessage.message;
self.numberOfLastMessageAuthor = friendNumForID(tempMessage.senderKey);
[self.tableView reloadData];
}

Expand Down
5 changes: 5 additions & 0 deletions Toxicity/TXCAppDelegate.h
Expand Up @@ -24,6 +24,11 @@ typedef NS_ENUM(NSUInteger, TXCThreadState) {
TXCThreadState_killed
};

typedef NS_ENUM(NSUInteger, TXCLocalNotification) {
TXCLocalNotification_friendMessage,
TXCLocalNotification_groupMessage
};

@interface TXCAppDelegate : UIResponder <UIApplicationDelegate, UIAlertViewDelegate>

@property (strong, nonatomic) UIWindow *window;
Expand Down
61 changes: 48 additions & 13 deletions Toxicity/TXCAppDelegate.m
Expand Up @@ -55,9 +55,25 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.toxWaitBufferSize = tox_wait_data_size();
self.toxWaitData = malloc(self.toxWaitBufferSize);


UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
application.applicationIconBadgeNumber = 0;
}

return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
if ([application applicationState] == UIApplicationStateActive) {
[[TWMessageBarManager sharedInstance] showMessageWithTitle:@"New Message"
description:notification.alertBody
type:TWMessageBarMessageTypeInfo];
}
application.applicationIconBadgeNumber = 0;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Expand Down Expand Up @@ -835,21 +851,30 @@ void print_message(Tox *m, int friendnumber, uint8_t * string, uint16_t length,
[theMessage setSenderKey:[[[[TXCSingleton sharedSingleton] mainFriendList] objectAtIndex:friendnumber] publicKey]];


//add to singleton
//if the message coming through is not to the currently opened chat window, then uialertview it
if (friendnumber != [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] row] && [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] section] != 1) {
// If the message coming through is not to the currently opened chat window, then fire a notification
if ((friendnumber != [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] row] &&
[[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] section] != 1) ||
[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground ||
[[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive) {
NSMutableArray *tempMessages = [[[[TXCSingleton sharedSingleton] mainFriendMessages] objectAtIndex:friendnumber] mutableCopy];
[tempMessages addObject:theMessage];

// Add message to singleton
[[TXCSingleton sharedSingleton] mainFriendMessages][friendnumber] = [tempMessages copy];

TXCFriendObject *tempFriend = [[[TXCSingleton sharedSingleton] mainFriendList] objectAtIndex:friendnumber];
[[TWMessageBarManager sharedInstance] showMessageWithTitle:[NSString stringWithFormat:@"Message from: %@", tempFriend.nickname]
description:@((char *)string)
type:TWMessageBarMessageTypeInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:TXCToxAppDelegateNotificationNewMessage object:theMessage userInfo:@{@"message":@((char *)string),@"friendNumber":@(friendnumber), @"friend":tempFriend} ];
// Fire a local notification for the message
UILocalNotification *friendMessageNotification = [[UILocalNotification alloc] init];
friendMessageNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
friendMessageNotification.alertBody = [NSString stringWithFormat:@"[%@]: %@", theMessage.senderName, theMessage.message];
friendMessageNotification.alertAction = @"show the message";
friendMessageNotification.timeZone = [NSTimeZone defaultTimeZone];
friendMessageNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:friendMessageNotification];
NSLog(@"Sent UILocalNotification: %@", friendMessageNotification.alertBody);

[[NSNotificationCenter defaultCenter] postNotificationName:TXCToxAppDelegateNotificationNewMessage object:theMessage];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:TXCToxAppDelegateNotificationNewMessage object:theMessage userInfo:@{@"message":@((char *)string),@"friendNumber":@(friendnumber)} ];
[[NSNotificationCenter defaultCenter] postNotificationName:TXCToxAppDelegateNotificationNewMessage object:theMessage];
}
});
}
Expand Down Expand Up @@ -882,15 +907,25 @@ void print_groupmessage(Tox *tox, int groupnumber, int friendgroupnumber, uint8_
theMessage.senderKey = [[[[TXCSingleton sharedSingleton] groupList] objectAtIndex:groupnumber] groupPulicKey];
//add to singleton
//if the message coming through is not to the currently opened chat window, then uialertview it
if (groupnumber != [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] row] && [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] section] != 0) {
if ((groupnumber != [[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] row] &&
[[[TXCSingleton sharedSingleton] currentlyOpenedFriendNumber] section] != 0) ||
[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground ||
[[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive) {
NSMutableArray *tempMessages = [[[[TXCSingleton sharedSingleton] groupMessages] objectAtIndex:groupnumber] mutableCopy];
[tempMessages addObject:theMessage];

// Add message to singleton
[[TXCSingleton sharedSingleton] groupMessages][groupnumber] = [tempMessages copy];

[[TWMessageBarManager sharedInstance] showMessageWithTitle:[NSString stringWithFormat:@"Message from Group #%d", groupnumber]
description:theirMessage
type:TWMessageBarMessageTypeSuccess];
// Fire a local notification for the message
UILocalNotification *groupMessageNotification = [[UILocalNotification alloc] init];
groupMessageNotification.fireDate = [NSDate date];
groupMessageNotification.alertBody = [NSString stringWithFormat:@"[Group %d][%@]: %@", groupnumber, theirName, theirMessage];
groupMessageNotification.alertAction = @"show the message";
groupMessageNotification.timeZone = [NSTimeZone defaultTimeZone];
groupMessageNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:groupMessageNotification];

} else {
[[NSNotificationCenter defaultCenter] postNotificationName:TXCToxAppDelegateNotificationNewMessage object:theMessage];
}
Expand Down

0 comments on commit 0dfcf92

Please sign in to comment.