Skip to content

Instantly share code, notes, and snippets.

@andkon
Created May 14, 2015 17:02
Show Gist options
  • Save andkon/ba567264c2ccfa8b54e4 to your computer and use it in GitHub Desktop.
Save andkon/ba567264c2ccfa8b54e4 to your computer and use it in GitHub Desktop.
ZeroPush notification setup
1. Add zeropush to heroku.
2. Get the pre-cert stuff in Apple Dev:
a) register the App ID/bundle ID
b) create a development cert
c) create a distribution cert
d) I think that's it?
3. Now get the aps certs. terminal:
pem -d
4. prod cert in terminal:
pem
5. in terminal:
heroku addons:open zeropush
6. then add the .pem files. PEM, not CER.
7.
Set up server:
1. install it:
pip install django-zeropush
pip freeze > requirements.txt
SETTINGS.requirements: 'zeropush'
2. add view, url:
from zeropush.models import PushDevice
class NotificationTokenView(APIView):
def post(self, request, format=None):
try:
token_string = request.DATA['token']
device, created = PushDevice.objects.get_or_create(token=token_string, user=request.user)
if created:
return Response(status=status.HTTP_201_CREATED)
elif device:
return Response(status=status.HTTP_200_OK)
except KeyError:
return Response(status=status.HTTP_404_NOT_FOUND)
url(r'^notifications/$', NotificationTokenView.as_view()),
3. Add stuff to settings:
ZEROPUSH_AUTH_TOKEN="asdf"
ZEROPUSH_NOTIFY_URL="https://api.zeropush.com/notify"
# though that's optional.
4. push to heroku:
git push heroku master
heroku run python manage.py migrate
Set up iOS:
1. Add delegate to AppDelegate.h:
#import <ZeroPush.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, ZeroPushDelegate>
2. Add postNotificationToken:
in .h:
- (void)postNotificationToken:(NSString *)token;
in .m:
- (void)postNotificationToken:(NSString *)token;
{
NSString *urlEnding = @"/api/notifications/";
NSDictionary *parameters = @{@"token": token};
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
[self.manager POST:urlEnding parameters:parameters success:^(NSURLSessionDataTask *task, id respo) {
NSLog(@"Successfully posted token %@ for current user %@", token, self.user.id);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error.description);
}];
}
3. Add stuff to didFinishLaunchingWithOptions:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
#if DEBUG
[ZeroPush engageWithAPIKey:ZeroPushDevToken delegate:self];
#else
[ZeroPush engageWithAPIKey:ZeroPushProdToken delegate:self];
#endif
[[ZeroPush shared] registerForRemoteNotifications];
3.5: add this above @interface in AppDelegate.m:
NSString * const NewMessagesNotification = @"NewMessagesNotification";
4. Add notification methods:
#pragma mark - Notifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)tokenData
{
// Call the convenience method registerDeviceToken, this helps us track device tokens for you
[[ZeroPush shared] registerDeviceToken:tokenData];
// This would be a good time to save the token and associate it with a user that you want to notify later.
NSString *tokenString = [ZeroPush deviceTokenFromData:tokenData];
NSLog(@"%@", tokenString);
// call notifications endpoint
[self postNotificationToken:tokenString];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"%@", [error description]);
//Common reason for errors:
// 1.) Simulator does not support receiving push notifications
// 2.) User rejected push alert
// 3.) "no valid 'aps-environment' entitlement string found for application"
// This means your provisioning profile does not have Push Notifications configured. https://zeropush.com/documentation/generating_certificates
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// called when Morrow is in the foreground. If we're logged in, get the message feed.
if (self.user != nil) {
// get MessagesVC to update the feed.
[[NSNotificationCenter defaultCenter] postNotificationName:NewMessagesNotification object:self];
}
}
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
// called when user presses a button on the notification
// the identifier ('category') is the name of a user state.
if ([identifier isEqualToString:@"CATEGORY_NAME"]) {
completionHandler();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment