WIP Notifications for ios

This commit is contained in:
akshayaurora 2018-01-31 04:15:51 +05:30
parent 0a568d3c8c
commit 25b2f29371
5 changed files with 117 additions and 0 deletions

View file

@ -0,0 +1,17 @@
from toolchain import CythonRecipe
class IosNotifRecipe(CythonRecipe):
version = "master"
url = "src"
library = "libnotifications.a"
pbx_frameworks = ["UserNotifications"]
depends = ["python"]
def install(self):
self.install_python_package(name="notifications.so", is_dir=False)
recipe = IosNotifRecipe()

View file

@ -0,0 +1,6 @@
#ifndef __IOS_NOTIF
#define __IOS_NOTIF
void notif(char *title, char *body);
#endif

View file

@ -0,0 +1,51 @@
#import <UserNotifications/UserNotifications.h>
#include "ios_notif.h"
void notif(char *title, char *body){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
else
NSLog(@"Already access granted");
}];
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
NSString *nstitle = [NSString stringWithCString:(char *)title encoding:NSUTF8StringEncoding];
NSString *nsbody = [NSString stringWithCString:(char *)body encoding:NSUTF8StringEncoding];
NSString *nsid = @"LocalNotification";
content.title = nstitle;
content.body = nsbody;
content.sound = [UNNotificationSound defaultSound];
UNNotificationAction *action = [UNNotificationAction
actionWithIdentifier:@"LAUNCH_ACTION"
title:@"Launch App"
options:UNNotificationActionOptionForeground];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"CAT_LAUNCH_ACTION"
actions:@[action] intentIdentifiers:@[]
options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];
[center setNotificationCategories:categories];
content.categoryIdentifier = @"CAT_LAUNCH_ACTION";
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5
repeats:false];
NSLog(@"Done adding trigger");
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:nsid
content:content trigger:trigger];
NSLog(@"Done initing notif request");
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong while adding to center");
}
else
NSLog(@"Done adding notif request");
}];
}

View file

@ -0,0 +1,30 @@
'''
Notifications module
====================
Wrapper for local notifications in iOS
'''
cdef extern from "ios_notif.h":
void notif(char *title, char *body)
class IOSNotif(object):
def show(self, title, body):
show_notif(title, body)
def show_notif(title, body):
'''
Show local notifications
:Parameters:
`title`: str
Title string
`body`: str
Body of the notification
Example for showing a local notification::
import notifications
title = "Title"
body = "Body"
notifications.IOSNotif().show(title, body)
'''
notif(title, body)

View file

@ -0,0 +1,13 @@
from distutils.core import setup, Extension
import os
setup(name='notifications',
version='1.0',
ext_modules=[
Extension(
'notifications', ['notifications.c', 'ios_notif.m'],
libraries=[],
library_dirs=[],
)
]
)