Skip to content

Commit

Permalink
Merge pull request #8 from JoaoCaixinha/master
Browse files Browse the repository at this point in the history
Adds push notifications support to RCTRealtimeMessaging
  • Loading branch information
realtime-framework committed Sep 8, 2015
2 parents 773b617 + 6df25a4 commit 857c1f4
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 181 deletions.
17 changes: 17 additions & 0 deletions RCTRealtimeMessaging/AppDelegate+RealtimeRCTPushNotifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppDelegate+RealtimeRCTPushNotifications.h
// RCTRealtimeMessaging
//
// Created by Joao Caixinha on 07/09/15.
// Copyright (c) 2015 Realtime. All rights reserved.
//
#import "AppDelegate.h"


@interface AppDelegate (RealtimeRCTPushNotifications)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

@end
81 changes: 81 additions & 0 deletions RCTRealtimeMessaging/AppDelegate+RealtimeRCTPushNotifications.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// AppDelegate+RealtimeRCTPushNotifications.m
// RCTRealtimeMessaging
//
// Created by Joao Caixinha on 07/09/15.
// Copyright (c) 2015 Realtime. All rights reserved.
//

#import "AppDelegate+RealtimeRCTPushNotifications.h"
#import <objc/runtime.h>

#pragma GCC diagnostic ignored "-Wundeclared-selector"
@implementation AppDelegate (RealtimeRCTPushNotifications)


+ (void)load
{
[[NSNotificationCenter defaultCenter] addObserver:[self class] selector:@selector(registForNotifications) name:UIApplicationDidFinishLaunchingNotification object:nil];
}


+ (BOOL)registForNotifications
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge];
}
#else
[application registerForRemoteNotificationTypes: UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge];
#endif

return YES;
}



- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"\n\n - didRegisterForRemoteNotificationsWithDeviceToken:\n%@\n", deviceToken);

id ortc = NSClassFromString (@"OrtcClient");
if ([ortc respondsToSelector:@selector(setDEVICE_TOKEN:)]) {
[ortc performSelector:@selector(setDEVICE_TOKEN:) withObject:[[NSString alloc] initWithString:newToken]];
}
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[self application:application didReceiveRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if ([userInfo objectForKey:@"C"] && [userInfo objectForKey:@"M"] && [userInfo objectForKey:@"A"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ApnsNotification" object:nil userInfo:userInfo];
}
}


- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to register with error : %@", error);

[[NSNotificationCenter defaultCenter] postNotificationName:@"ApnsRegisterError" object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"ApnsRegisterError"]];
}





@end
3 changes: 1 addition & 2 deletions RCTRealtimeMessaging/OrtcClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,7 @@ - (id)initWithConfig:(id<OrtcClientDelegate>) aDelegate
- (void) receivedNotification:(NSNotification *) notification
{
// [notification name] should be @"ApnsNotification" for received Apns Notififications
if ([[notification name] isEqualToString:@"ApnsNotification"]) {

if ([[notification name] isEqualToString:@"ApnsNotification"]) {
NSDictionary *notificaionInfo = [[NSDictionary alloc] initWithDictionary:[notification userInfo]];
if ([[notificaionInfo objectForKey:@"A"] isEqualToString:applicationKey]) {

Expand Down
2 changes: 2 additions & 0 deletions RCTRealtimeMessaging/RCTRealtimeMessaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
@interface RCTRealtimeMessaging : NSObject<OrtcClientDelegate, RCTBridgeModule>
@property(retain, nonatomic)OrtcClient *ortcClient;
@property(retain, nonatomic)NSMutableDictionary *queue;
@property(retain, nonatomic)NSDictionary *pushInfo;

@end
76 changes: 76 additions & 0 deletions RCTRealtimeMessaging/RCTRealtimeMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,82 @@ @implementation RCTRealtimeMessaging

RCT_EXPORT_MODULE()


- (id)init{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedNotification:) name:@"Notification" object:nil];
}
return self;
}

- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void) receivedNotification:(NSNotification *) notification{
NSDictionary *userInfo = [notification userInfo];
NSMutableDictionary *pushInfo = [[NSMutableDictionary alloc] init];
[self handleCustom:pushInfo from:userInfo];
_pushInfo = [pushInfo objectForKey:@"payload"];
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onPushNotification"
body:[pushInfo objectForKey:@"payload"]];
}

RCT_EXPORT_METHOD(checkForNotifications){
if(_pushInfo){
[self.bridge.eventDispatcher sendDeviceEventWithName:@"onPushNotification"
body:_pushInfo];
_pushInfo = nil;
}
}


- (void)handleStd:(NSMutableDictionary*)pushInfo from:(NSDictionary*)userInfo
{
NSString* msg = [userInfo objectForKey:@"M"];
int num = 0;
NSUInteger len = [msg length];
unichar buffer[len+1];
[msg getCharacters: buffer range: NSMakeRange(0, len)];

NSString *finalM;
for (int i=0; i<len; i++) {
if (buffer[i] == '_') {
num++;
if (num == 2 && len > i + 1) {
finalM = [msg substringFromIndex:i+1];
}
}
}

NSError *error = nil;
NSData *jsonData = [finalM dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

if (json != nil) {
[pushInfo setObject:json forKey:@"payload"];
}else
{
[pushInfo setObject:finalM forKey:@"payload"];
}
}


- (void)handleCustom:(NSMutableDictionary*)pushInfo from:(NSDictionary*)userInfo
{
NSMutableDictionary *payload = [[NSMutableDictionary alloc] init];
for (NSString* key in [[userInfo objectForKey:@"aps"] allKeys]) {
if (![key isEqualToString:@"sound"] && ![key isEqualToString:@"badge"] && ![key isEqualToString:@"alert"]) {
[payload setObject:[[userInfo objectForKey:@"aps"] objectForKey:key] forKey:key];
}
}
[pushInfo setObject:payload forKey:@"payload"];
}




RCT_EXPORT_METHOD(connect:(id)connectionSettings id:(NSString*)pId){

NSString *appKey = [RCTConvert NSString:connectionSettings[@"appKey"]];
Expand Down
84 changes: 0 additions & 84 deletions RCTRealtimeMessaging/RealtimePushAppDelegate.h

This file was deleted.

93 changes: 0 additions & 93 deletions RCTRealtimeMessaging/RealtimePushAppDelegate.m

This file was deleted.

0 comments on commit 857c1f4

Please sign in to comment.