mirror of
https://github.com/cupcakearmy/fight-of-the-mobiles.git
synced 2026-04-02 10:15:23 +00:00
69 lines
3.0 KiB
Objective-C
69 lines
3.0 KiB
Objective-C
|
|
@import UserNotifications;
|
|
|
|
#import <Cordova/CDV.h>
|
|
#import "W3CLocalNotifications.h"
|
|
|
|
@implementation W3CLocalNotifications : CDVPlugin
|
|
|
|
@synthesize callbackId;
|
|
|
|
- (void)show:(CDVInvokedUrlCommand*)command {
|
|
self.callbackId = command.callbackId;
|
|
|
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
|
|
content.title = [command.arguments objectAtIndex:0];
|
|
content.body = [command.arguments objectAtIndex:3];
|
|
|
|
NSString *identifier = [command.arguments objectAtIndex:4];
|
|
|
|
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:nil];
|
|
|
|
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
|
|
if (error != nil) {
|
|
NSLog(@"Something went wrong: %@",error);
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
} else {
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"show"];
|
|
[pluginResult setKeepCallbackAsBool:YES];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (void)close:(CDVInvokedUrlCommand*)command {
|
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
|
NSArray *identifiers = @[[command.arguments objectAtIndex:0]];
|
|
[center removeDeliveredNotificationsWithIdentifiers:identifiers];
|
|
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
}
|
|
|
|
- (void)requestPermission:(CDVInvokedUrlCommand*)command {
|
|
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
|
|
UNUserNotificationCenter *center = UNUserNotificationCenter.currentNotificationCenter;
|
|
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
if (granted) {
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"granted"];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
} else {
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"denied"];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (void)notificationClicked {
|
|
NSLog(@"in plugin, local notification clicked");
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"click"];
|
|
[pluginResult setKeepCallbackAsBool:YES];
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
|
|
}
|
|
|
|
@end
|