Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'background'
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit committed Jul 26, 2014
2 parents 9ce7a81 + 88b63aa commit 660a39d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
7 changes: 7 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<js-module src="www/aerogear-push.js" name="AeroGear.UnifiedPush">
<clobbers target="push"/>
</js-module>
<dependency id="android.support.v4" url="https://github.com/MobileChromeApps/cordova-plugin-android-support-v4" />
<dependency id="com.google.playservices" url="https://github.com/MobileChromeApps/google-play-services" />
<platform name="android">
<dependency id="android.support.v4" url="https://github.com/MobileChromeApps/cordova-plugin-android-support-v4"/>
<dependency id="com.google.playservices" url="https://github.com/MobileChromeApps/google-play-services"/>
Expand Down Expand Up @@ -54,6 +56,11 @@
<param name="ios-package" value="PushPlugin"/>
</feature>
</config-file>
<config-file target="*-Info.plist" parent="UIBackgroundModes">
<array>
<string>remote-notification</string>
</array>
</config-file>
<source-file src="src/ios/AppDelegate+notification.m"/>
<source-file src="src/ios/PushPlugin.m"/>
<header-file src="src/ios/AppDelegate+notification.h"/>
Expand Down
14 changes: 12 additions & 2 deletions src/ios/AppDelegate+notification.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ - (AppDelegate *)swizzled_init
// to process notifications in cold-start situations
- (void)createNotificationChecker:(NSNotification *)notification
{
if (notification)
{
if (notification) {
NSDictionary *launchOptions = [notification userInfo];
if (launchOptions)
self.launchNotification = [launchOptions objectForKey: @"UIApplicationLaunchOptionsRemoteNotificationKey"];
Expand Down Expand Up @@ -92,6 +91,17 @@ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(N
}
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// if the user clicked the notification, we know that the callback has already been called, so we simply return.
if (application.applicationState == UIApplicationStateInactive) {
return;
}

PushPlugin *pushHandler = [self getCommandInstance:@"PushPlugin"];
[pushHandler backgroundFetch:completionHandler userInfo:userInfo];
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

NSLog(@"active");
Expand Down
5 changes: 5 additions & 0 deletions src/ios/PushPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
@property(nonatomic, strong) NSDictionary *notificationMessage;
@property BOOL isInline;

@property(nonatomic, strong) NSMutableArray *completionHandlers;

- (void)register:(CDVInvokedUrlCommand *)command;

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
Expand All @@ -38,4 +40,7 @@

- (void)notificationReceived;

- (void)setContentAvailable:(CDVInvokedUrlCommand *)command;

- (void)backgroundFetch:(void (^)(UIBackgroundFetchResult))handler userInfo:(NSDictionary *)userInfo;
@end
21 changes: 21 additions & 0 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ - (void)notificationReceived {
}
}

- (void)setContentAvailable:(CDVInvokedUrlCommand*)command {
NSLog(@"setContentAvailable");
if ([self.completionHandlers count]) {
NSMutableDictionary *options = [command.arguments objectAtIndex:0];
NSString *type = [options objectForKey:@"type"];
void (^handler)(UIBackgroundFetchResult) = [self.completionHandlers objectAtIndex:0];
handler((UIBackgroundFetchResult) [type intValue]);
[self.completionHandlers removeObject:handler];
}
}

- (void)backgroundFetch:(void (^)(UIBackgroundFetchResult))handler userInfo:(NSDictionary *)userInfo {
NSLog(@"Background Fetch");
if (!self.completionHandlers) {
self.completionHandlers = [[NSMutableArray alloc] init];
}
[self.completionHandlers addObject:handler];
self.notificationMessage = userInfo;
[self notificationReceived];
}

- (void)setApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command; {
DLog(@"setApplicationIconBadgeNumber:%@\n withDict:%@", arguments, options);

Expand Down
26 changes: 23 additions & 3 deletions www/aerogear-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ var exec = require("cordova/exec");
* This is a global variable called push exposed by cordova
* @status Stable
* @constructs Push
*/
var Push = function(){};
*/
function Push(){
this.FetchResult = {
NewData: 0,
NoData: 1,
Failed: 2
}
}

/**
Registers the device with the APNS (iOS) or GCM (Android) and the Unified Push server.
Expand Down Expand Up @@ -133,4 +139,18 @@ Push.prototype.setApplicationIconBadgeNumber = function (successCallback, badge)
]);
};

module.exports = new Push();
/**
* Call this function to tell the OS if there was data or not so it can schedule the next fetch operation
* @param {int} dataType - one of the BackgroundFetchResults or 0 new data 1 no data or 2 failed
* @returns {void}
*/
Push.prototype.setContentAvailable = function(dataType) {
return exec(null, null, "PushPlugin", "setContentAvailable", [{type: dataType}]);
};

var push = new Push();
if (Object.freeze) {
Object.freeze(push.FetchResult);
}

module.exports = push;

0 comments on commit 660a39d

Please sign in to comment.