Skip to content

Commit

Permalink
Started work in push for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
sidiabale committed Sep 30, 2015
1 parent ca3a8b2 commit ef6f3b4
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 6 deletions.
3 changes: 3 additions & 0 deletions codenameone_library_appended.properties
Expand Up @@ -3,3 +3,6 @@
#is an appended type property.
#
#Wed Jan 09 17:59:31 IST 2013

# Required libs for Parse iOS native SDK (see also https://parse.com/apps/quickstart#parse_push/ios/native/existing)
codename1.arg.ios.add_libs=Foundation.framework;AudioToolbox.framework;CFNetwork.framework;CoreGraphics.framework;CoreLocation.framework;QuartzCore.framework;Security.framework;StoreKit.framework;SystemConfiguration.framework;Accounts.framework;Social.framework;libz.dylib;libsqlite3.dylib;Parse.a;Bolts.a
2 changes: 1 addition & 1 deletion codenameone_library_required.properties
Expand Up @@ -5,4 +5,4 @@
#needs the google play services but the main project marked this property to false
#The refresh cn1lib action will fail.
#
#Wed Jan 09 17:59:31 IST 2013
#Wed Jan 09 17:59:31 IST 2013
1 change: 1 addition & 0 deletions test/CN1TestApp/codenameone_settings.properties
Expand Up @@ -5,6 +5,7 @@ codename1.ios.release.provision=D\:\\Workspace\\projects\\parse4cn1\\test\\CN1Te
codename1.arg.rim.obfuscation=false
codename1.j2me.nativeTheme=nbproject/nativej2me.res
codename1.arg.ios.project_type=ios
codename1.arg.ios.glAppDelegateHeader=\#import "Parse.h"
codename1.arg.ios.interface_orientation=UIInterfaceOrientationPortrait\:UIInterfaceOrientationPortraitUpsideDown\:UIInterfaceOrientationLandscapeLeft\:UIInterfaceOrientationLandscapeRight
codename1.displayName=parse4cn1TestApp
guiResource=theme.res
Expand Down
5 changes: 0 additions & 5 deletions test/ParsePushTestApp/HowTo_EnablePushForAndroid.txt
Expand Up @@ -54,11 +54,6 @@ android.xpermissions =
android:name="com.parse4cn1.TestApp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.parse4cn1.TestApp.permission.C2D_MESSAGE" />


Add activity attribute

android.xactivity=android:alwaysRetainTaskState="true"

So that activity is opened in existing task when push message is opened.

2. Invoke <TODO: Method name> to enable push support
Expand Down
153 changes: 153 additions & 0 deletions test/ParsePushTestApp/HowTo_EnablePushForIOS.txt
@@ -0,0 +1,153 @@
Tutorial: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

Issue: no valid aps:
1. https://www.google.nl/webhp?sourceid=chrome-instant&rlz=1C1CHFX_enNL640NL640&ion=1&espv=2&ie=UTF-8#q=no%20valid%20aps-environment%20entitlement%20string%20found%20for%20application
2. Manually signing http://stackoverflow.com/questions/5681172/bundle-identifier-and-push-certificate-aps-environment-entitlement-error
http://stackoverflow.com/questions/15634188/resigning-an-ios-provisioning-profile
http://stackoverflow.com/questions/6896029/re-sign-ipa-iphone
- Initial result: code object is not signed at all

Error: "no valid 'aps-environment' entitlement string found for application"

Tips
- Registration for push different in iOS 8: http://stackoverflow.com/questions/4086599/why-didregisterforremotenotificationswithdevicetoken-is-not-called

Device token registration not working
- (@"" trick) http://stackoverflow.com/questions/31116849/parse-com-devicetoken-and-pfinstallation-not-saved See also: http://stackoverflow.com/questions/31181428/parse-installation-table-not-registering-devicetoken


Issues: Creating certificates
1. Unknown authority --> Export to .p12 option not visible
http://stackoverflow.com/questions/13820680/this-certificate-was-signed-by-an-unknown-authority

2. No certificate found (instructions from Parse are not crystal clear). You need a new separate one for this purpose
http://stackoverflow.com/questions/25223644/no-certificates-are-available-when-adding-ios-provisioning-profile-for-parse-p

=================
Steps


// ios.add_libs
Foundation.framework;AudioToolbox.framework;CFNetwork.framework;CoreGraphics.framework;CoreLocation.framework;QuartzCore.framework;Security.framework;StoreKit.framework;SystemConfiguration.framework;libz.dylib;libsqlite3.dylib;Parse.a;Bolts.a

//Extra add_libs: Parse.a and Bolts.a (renamed from original non .a files)

// If you're using the -ObjC linker flag required by some third-party libraries, add these as well:
// ios.add_libs
Accounts.framework;Social.framework

// Also remember to enable -ObjC linker flag with
// ios.objC

// ios.glAppDelegateHeader
#import "Parse.h" // <Parse/Parse.h> --> "Parse.h" since included libraries are flattened


// ios.afterFinishLaunching
[Parse setApplicationId:@"j1KMuH9otZlHcPncU9dZ1JFH7cXL8K5XUiQQ9ot8" clientKey:@"V6ZUyBtfERtzbq6vjeAb13tiFYij980HN9nQTWGB"];

UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this. This action cannot be undone" delegate:nil cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert autorelease];

// ios.glAppDelegateBody
// Following three methods should be added without line breaks:


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; currentInstallation.channels = @[@"global"];[currentInstallation saveInBackground];} - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { if (error.code == 3010) { NSLog(@"Push notifications are not supported in the iOS Simulator.");} else { NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);}} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {[PFPush handlePush:userInfo];}

// With line breaks for readability of tutorial

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.deviceToken = @""; // Tip from stackoverflow!!!
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = @[@"global"];
[currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
if (error.code == 3010) {
NSLog(@"Push notifications are not supported in the iOS Simulator.");
} else {
// show some alert or otherwise handle the failure to register.
NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
}
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
}

// With popups


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; currentInstallation.channels = @[@"global"]; [currentInstallation saveInBackground]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Installation" message:@"Installation saved in background" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; } - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; if (error.code == 3010) { NSLog(@"Push notifications are not supported in the iOS Simulator."); } else { NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error); }} - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Received push while app was running" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; [PFPush handlePush:userInfo];}

// With debugging

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation.deviceToken = @""; // Tip from stackoverflow!!!
[currentInstallation setDeviceTokenFromData:deviceToken];
currentInstallation.channels = @[@"global"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Device token" message:currentInstallation.deviceToken delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
[alert autorelease];

[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Installation saved" message:@"Installation saved in background" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
[alert autorelease];

}

if (error) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Installation not saved" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
[alert autorelease];

}
}];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alert show];
[alert autorelease];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Received push while app was running" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show];
[alert autorelease];
[PFPush handlePush:userInfo];
}


// Compressed

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { PFInstallation *currentInstallation = [PFInstallation currentInstallation]; currentInstallation.deviceToken = @""; [currentInstallation setDeviceTokenFromData:deviceToken]; currentInstallation.channels = @[@"global"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Device token" message:currentInstallation.deviceToken delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (succeeded) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Installation saved" message:@"Installation saved in background" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; } if (error) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Installation not saved" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; }}];} - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Registration Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Received push while app was running" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; [alert show]; [alert autorelease]; [PFPush handlePush:userInfo]; }


Utils
- NSDictionary to JSON string
http://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary


Receiving push in different app states
http://stackoverflow.com/questions/23168345/detect-if-application-didreceiveremotenotification-fetchcompletionhandler-was
http://stackoverflow.com/questions/31450403/didreceiveremotenotification-not-working-in-the-background
https://parse.com/docs/ios/guide#push-notifications-receiving-pushes
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html

0 comments on commit ef6f3b4

Please sign in to comment.