Skip to content

Commit

Permalink
Merge pull request #1777 from joeywatts/quick-reply
Browse files Browse the repository at this point in the history
Add support for interactive notifications (fixes #625)
  • Loading branch information
manuroe committed Feb 26, 2018
2 parents 563714c + 31e3501 commit d91e2f9
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion Riot/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,24 @@ - (void)registerUserNotificationSettings
{
if (!isPushRegistered)
{
NSMutableSet* notificationCategories = [NSMutableSet set];
if ([[UIMutableUserNotificationAction class] instancesRespondToSelector:@selector(behavior)])
{
UIMutableUserNotificationAction* quickReply = [[UIMutableUserNotificationAction alloc] init];
quickReply.title = NSLocalizedStringFromTable(@"room_message_short_placeholder", @"Vector", nil);
quickReply.identifier = @"inline-reply";
quickReply.activationMode = UIUserNotificationActivationModeBackground;
quickReply.authenticationRequired = true;
quickReply.behavior = UIUserNotificationActionBehaviorTextInput;

UIMutableUserNotificationCategory* quickReplyCategory = [[UIMutableUserNotificationCategory alloc] init];
quickReplyCategory.identifier = @"QUICK_REPLY";
[quickReplyCategory setActions:[NSArray arrayWithObjects:quickReply, nil] forContext:UIUserNotificationActionContextDefault];
[notificationCategories addObject:quickReplyCategory];
}

// Registration on iOS 8 and later
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:nil];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:notificationCategories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
}
Expand Down Expand Up @@ -1127,6 +1143,56 @@ - (void)application:(UIApplication *)application didRegisterUserNotificationSett
}
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
if ([identifier isEqualToString: @"inline-reply"])
{
NSString* roomId = notification.userInfo[@"room_id"];
if (roomId.length)
{
NSArray* mxAccounts = [MXKAccountManager sharedManager].activeAccounts;
MXKRoomDataSource* roomDataSource = nil;
for (MXKAccount* account in mxAccounts)
{
MXRoom* room = [account.mxSession roomWithRoomId:roomId];
if (room)
{
MXKRoomDataSourceManager* manager = [MXKRoomDataSourceManager sharedManagerForMatrixSession:account.mxSession];
if (manager)
{
roomDataSource = [manager roomDataSourceForRoom:roomId create:YES];
}
break;
}
}
if (roomDataSource == nil)
{
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: room with id %@ not found", roomId);
}
else
{
NSString* responseText = [responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey];
if (responseText != nil && responseText.length != 0)
{
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: sending message to room: %@", roomId);
[roomDataSource sendTextMessage:responseText success:^(NSString* eventId) {} failure:^(NSError* error) {
UILocalNotification* failureNotification = [[UILocalNotification alloc] init];
failureNotification.alertBody = NSLocalizedStringFromTable(@"room_event_failed_to_send", @"Vector", nil);
failureNotification.userInfo = notification.userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification: failureNotification];
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: error sending text message: %@", error);
}];
}
}
}
}
else
{
NSLog(@"[AppDelegate][Push] handleActionWithIdentifier: unhandled identifier %@", identifier);
}
completionHandler();
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"[AppDelegate][Push] didReceiveLocalNotification: applicationState: %@", @(application.applicationState));
Expand Down Expand Up @@ -1380,6 +1446,12 @@ - (void)handleLocalNotificationsForAccount:(MXKAccount*)account
@"user_id": account.mxCredentials.userId
};

BOOL isNotificationContentShown = !event.isEncrypted || account.showDecryptedContentInNotifications;
if ((event.eventType == MXEventTypeRoomMessage || event.eventType == MXEventTypeRoomEncrypted) && isNotificationContentShown)
{
eventNotification.category = @"QUICK_REPLY";
}

// Set sound name based on the value provided in action of MXPushRule
for (MXPushRuleAction *action in rule.actions)
{
Expand Down

0 comments on commit d91e2f9

Please sign in to comment.