Skip to content

Instantly share code, notes, and snippets.

@javieranton-zz
Last active October 18, 2020 20:21
Show Gist options
  • Save javieranton-zz/86033f1e0c42dae86e936407e4680fb5 to your computer and use it in GitHub Desktop.
Save javieranton-zz/86033f1e0c42dae86e936407e4680fb5 to your computer and use it in GitHub Desktop.
/*
.h file
*/
#import <Foundation/Foundation.h>
#import <UserNotifications/UserNotifications.h>
@interface your_namespace_YourInterfaceImpl : NSObject<UNUserNotificationCenterDelegate> {
}
-(void)registerPush;
-(BOOL)isSupported;
@end
/*
.m file
*/
#import "your_namespace_YourInterfaceImpl.h"
#include "your_namespace_MainClass.h"
#include "CodenameOne_GLViewController.h"
@implementation your_namespace_YourInterfaceImpl
-(BOOL)isSupported{
return YES;
}
-(void)registerPush{ if (@available(iOS 10, *)) {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}else {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
//add below 2 voids. They aren't CN1 interface methods, just override the parent class
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment