Skip to content

Commit

Permalink
methods for getting remote notification permission status and existin…
Browse files Browse the repository at this point in the history
…g registration
  • Loading branch information
owenpshaw committed Feb 8, 2024
1 parent 53d0298 commit 43a65ef
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
93 changes: 92 additions & 1 deletion Frameworks/NotificationKit/NKHTMLUserNotificationCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ JSClass("NKHTMLUserNotificationCenter", NKUserNotificationCenter, {
}, function(error){
completion.call(target, null, error);
});
}, function(error){
completion.call(target, null, error);
});
}
return completion.promise;
Expand Down Expand Up @@ -115,6 +117,94 @@ JSClass("NKHTMLUserNotificationCenter", NKUserNotificationCenter, {
}, function(error){
completion.call(target, error);
});
}, function(error){
completion.call(target, error);
});
}
return completion.promise;
},

getRemoteNotificationRegistration: function(completion, target){
if (!completion){
completion = Promise.completion(Promise.rejectNonNullSecondArgument);
}
var notificationCenter = this;
if (this.serviceWorkerContainer === null){
JSRunLoop.main.schedule(completion, target, null, null);
}else{
this.serviceWorkerContainer.getRegistration().then(function(serviceWorkerRegistration){
if (!serviceWorkerRegistration){
completion.call(target, null, null);
return;
}
if (!serviceWorkerRegistration.pushManager){
completion.call(target, null, null);
return;
}
serviceWorkerRegistration.pushManager.getSubscription().then(function(subscription){
if (subscription){
var registration = {
type: NKUserNotificationCenter.RegistrationType.web,
options: {
applicationServerJWK: notificationCenter.webPushApplicationServerJWK,
},
subscription: {
endpoint: subscription.endpoint,
expirationTime: subscription.expirationTime,
keys: {
p256dh: JSData.initWithBuffer(subscription.getKey("p256dh")).base64URLStringRepresentation(),
auth: JSData.initWithBuffer(subscription.getKey("auth")).base64URLStringRepresentation()
},
}
};
completion.call(target, registration, null);
}else{
completion.call(target, null, null);
}
}, function(error){
completion.call(target, null, error);
});
}, function(error){
completion.call(target, null, error);
});
}
return completion.promise;
},

getRemoteNotificationAuthorizationStatus: function(completion, target){
if (!completion){
completion = Promise.completion();
}
var notificationCenter = this;
if (this.serviceWorkerContainer === null){
JSRunLoop.main.schedule(completion, target, NKUserNotificationCenter.AuthorizationStatus.unavailable);
}else{
this.serviceWorkerContainer.getRegistration().then(function(serviceWorkerRegistration){
if (!serviceWorkerRegistration){
completion.call(target, NKUserNotificationCenter.AuthorizationStatus.unavailable);
return;
}
if (!serviceWorkerRegistration.pushManager){
completion.call(target, NKUserNotificationCenter.AuthorizationStatus.unavailable);
return;
}
var options = {
userVisibleOnly: true
};
if (notificationCenter.webPushApplicationServerJWK){
options.applicationServerKey = JSData.initWithChunks([
JSData.initWithArray([0x04]),
notificationCenter.webPushApplicationServerJWK.x.dataByDecodingBase64URL(),
notificationCenter.webPushApplicationServerJWK.y.dataByDecodingBase64URL()
]).base64URLStringRepresentation();
}
serviceWorkerRegistration.pushManager.permissionState(options).then(function(permissionState){
completion.call(target, statusByPermission[permissionState]);
}, function(error){
completion.call(target, NKUserNotificationCenter.AuthorizationStatus.unknown);
});
}, function(error){
completion.call(target, NKUserNotificationCenter.AuthorizationStatus.unknown);
});
}
return completion.promise;
Expand Down Expand Up @@ -220,7 +310,7 @@ JSClass("NKHTMLUserNotificationCenter", NKUserNotificationCenter, {
if (!completion){
completion = Promise.completion();
}
var status = NKUserNotificationCenter.AuthorizationStatus.denied;
var status = NKUserNotificationCenter.AuthorizationStatus.unavailable;
if (this.supportsHTMLNotifications){
status = statusByPermission[Notification.permission];
}
Expand Down Expand Up @@ -259,6 +349,7 @@ JSClass("NKHTMLUserNotificationCenter", NKUserNotificationCenter, {

var statusByPermission = {
"default": NKUserNotificationCenter.AuthorizationStatus.unknown,
"prompt": NKUserNotificationCenter.AuthorizationStatus.unknown,
"granted": NKUserNotificationCenter.AuthorizationStatus.authorized,
"denied": NKUserNotificationCenter.AuthorizationStatus.denied,
};
Expand Down
9 changes: 8 additions & 1 deletion Frameworks/NotificationKit/NKUserNotificationCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ JSClass("NKUserNotificationCenter", JSObject, {
unregisterForRemoteNotifications: function(completion, target){
},

getRemoteNotificationRegistration: function(completion, target){
},

getRemoteNotificationAuthorizationStatus: function(completion, target){
},

addNotification: function(notification, date){
},

Expand All @@ -54,7 +60,8 @@ JSClass("NKUserNotificationCenter", JSObject, {
NKUserNotificationCenter.AuthorizationStatus = {
unknown: 0,
authorized: 1,
denied: 2
denied: 2,
unavailable: 3,
};

NKUserNotificationCenter.RegistrationType = {
Expand Down

0 comments on commit 43a65ef

Please sign in to comment.