Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit a5d4f01

Browse files
committed
feature: enable handling input notifications
1 parent bc9ee1b commit a5d4f01

File tree

4 files changed

+56
-23
lines changed

4 files changed

+56
-23
lines changed

demo/app/messaging-view-model.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export class MessagingViewModel {
4747
title: "Not on lock screen",
4848
options: messaging.IosInteractiveNotificationActionOptions.authenticationRequired
4949
},
50+
{
51+
identifier: "INPUT_ACTION",
52+
title: "Reply",
53+
options: messaging.IosInteractiveNotificationActionOptions.foreground,
54+
type: "input",
55+
submitLabel: "Fire!",
56+
placeholder: "Load the gun..."
57+
},
5058
{
5159
identifier: "DELETE_ACTION",
5260
title: "Delete and open",
@@ -56,17 +64,15 @@ export class MessagingViewModel {
5664
model.iosSettings.interactiveSettings.categories = [
5765
{
5866
identifier: "GENERAL"
59-
// actionsForDefaultContext: ['OPEN_ACTION', 'IGNORE_ACTION', 'DELETE_ACTION'],
60-
// actionsForMinimalContext: ['OPEN_ACTION', 'IGNORE_ACTION', 'DELETE_ACTION']
6167
}
6268
];
6369

64-
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message) => {
70+
model.onNotificationActionTakenCallback = (actionIdentifier: string, message: firebase.Message, inputText?: string) => {
6571
console.log(`onNotificationActionTakenCallback fired! \n\r Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`);
6672

6773
alert({
6874
title: "Interactive push action",
69-
message: `Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}`,
75+
message: `Message: ${JSON.stringify(message)}, \n\r Action taken: ${actionIdentifier}` + (inputText ? `, \n\r Input text: ${inputText}` : ""),
7076
okButtonText: "Nice!"
7177
});
7278
};

src/firebase.ios.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,7 @@ firebase.firestore.Transaction = (nativeTransaction: FIRTransaction): firestore.
17761776
};
17771777

17781778
firebase.firestore.runTransaction = (updateFunction: (transaction: firestore.Transaction) => Promise<any>): Promise<void> => {
1779-
return new Promise((resolve, reject) => {
1779+
return new Promise<void>((resolve, reject) => {
17801780
FIRFirestore.firestore().runTransactionWithBlockCompletion(
17811781
(nativeTransaction: FIRTransaction, err: any) => {
17821782
const tx = new firebase.firestore.Transaction(nativeTransaction);
@@ -1930,7 +1930,7 @@ firebase.firestore.add = (collectionPath: string, document: any): Promise<firest
19301930
};
19311931

19321932
firebase.firestore.set = (collectionPath: string, documentPath: string, document: any, options?: firestore.SetOptions): Promise<void> => {
1933-
return new Promise((resolve, reject) => {
1933+
return new Promise<void>((resolve, reject) => {
19341934
try {
19351935
if (typeof(FIRFirestore) === "undefined") {
19361936
reject("Make sure 'Firebase/Firestore' is in the plugin's Podfile");
@@ -1988,7 +1988,7 @@ function fixSpecialFields(item) {
19881988
}
19891989

19901990
firebase.firestore.update = (collectionPath: string, documentPath: string, document: any): Promise<void> => {
1991-
return new Promise((resolve, reject) => {
1991+
return new Promise<void>((resolve, reject) => {
19921992
try {
19931993
if (typeof(FIRFirestore) === "undefined") {
19941994
reject("Make sure 'Firebase/Firestore' is in the plugin's Podfile");
@@ -2016,7 +2016,7 @@ firebase.firestore.update = (collectionPath: string, documentPath: string, docum
20162016
};
20172017

20182018
firebase.firestore.delete = (collectionPath: string, documentPath: string): Promise<void> => {
2019-
return new Promise((resolve, reject) => {
2019+
return new Promise<void>((resolve, reject) => {
20202020
try {
20212021
if (typeof(FIRFirestore) === "undefined") {
20222022
reject("Make sure 'Firebase/Firestore' is in the plugin's Podfile");

src/messaging/messaging.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ export declare class IosInteractivePushSettings {
3030
foreground = 4,
3131
}
3232

33-
export declare interface IosInteractiveNotificationAction {
33+
export interface IosInteractiveNotificationAction {
3434
identifier: string;
3535
title: string;
36-
// activationMode?: string;
37-
// destructive?: boolean;
38-
// authenticationRequired?: boolean;
36+
type: string;
37+
submitLabel: string;
38+
placeholder: string;
3939
options?: IosInteractiveNotificationActionOptions;
4040
behavior?: string;
41-
}
41+
}
4242

4343
export declare interface IosInteractiveNotificationCategory {
4444
identifier: string;

src/messaging/messaging.ios.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,32 @@ export function addBackgroundRemoteNotificationHandler(appDelegate) {
143143
};
144144
}
145145

146-
export function registerForInteractivePush(model?: any) {
146+
export function registerForInteractivePush(model?: PushNotificationModel) {
147147
let nativeActions: Array<UNNotificationAction> = [];
148148

149-
model.iosSettings.interactiveSettings.actions.forEach(action => {
150-
let nativeAction = UNNotificationAction.actionWithIdentifierTitleOptions(action.identifier, action.title, action.options | UNNotificationActionOptionNone);
149+
model.iosSettings.interactiveSettings.actions.forEach(((action: IosInteractiveNotificationAction) => {
150+
let notificationActionOptions: UNNotificationActionOptions = action.options ? <UNNotificationActionOptions>action.options.valueOf() : UNNotificationActionOptionNone;
151+
let actionType = action.type || "button";
152+
let nativeAction: UNNotificationAction;
153+
154+
if (actionType === "input") {
155+
nativeAction = UNTextInputNotificationAction.actionWithIdentifierTitleOptionsTextInputButtonTitleTextInputPlaceholder(
156+
action.identifier,
157+
action.title,
158+
notificationActionOptions,
159+
action.submitLabel || "Submit",
160+
action.placeholder);
161+
} else if (actionType === "button") {
162+
nativeAction = UNNotificationAction.actionWithIdentifierTitleOptions(
163+
action.identifier,
164+
action.title,
165+
notificationActionOptions);
166+
} else {
167+
console.log("Unsupported action type: " + action.type);
168+
}
151169

152170
nativeActions.push(nativeAction);
153-
});
171+
}));
154172

155173
let actions: NSArray<UNNotificationAction> = <NSArray<UNNotificationAction>>NSArray.arrayWithArray(<any>nativeActions);
156174
let nativeCategories: Array<UNNotificationCategory> = [];
@@ -273,6 +291,9 @@ export enum IosInteractiveNotificationActionOptions {
273291
export interface IosInteractiveNotificationAction {
274292
identifier: string;
275293
title: string;
294+
type: string;
295+
submitLabel: string;
296+
placeholder: string;
276297
// activationMode?: string;
277298
// destructive?: boolean;
278299
// authenticationRequired?: boolean;
@@ -303,6 +324,11 @@ export class PushNotificationModel {
303324
iosSettings: IosPushSettings;
304325
onNotificationActionTakenCallback: Function;
305326
}
327+
export class NotificationActionResponse {
328+
androidSettings: any;
329+
iosSettings: IosPushSettings;
330+
onNotificationActionTakenCallback: Function;
331+
}
306332

307333
export function areNotificationsEnabled() {
308334
let app = iOSUtils.getter(UIApplication, UIApplication.sharedApplication);
@@ -345,7 +371,7 @@ function _registerForRemoteNotifications() {
345371
}
346372
});
347373

348-
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initWithCallback((unnotification, actionIdentifier?) => {
374+
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initWithCallback((unnotification, actionIdentifier?, inputText?) => {
349375
// if the app is in the foreground then this method will receive the notification
350376
// if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
351377
// if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
@@ -371,7 +397,7 @@ function _registerForRemoteNotifications() {
371397
userInfoJSON.aps = undefined;
372398
// TODO: THIS CODE UP IS DUPLICATE, REFACTOR!!!!
373399

374-
_notificationActionTakenCallback(actionIdentifier, userInfoJSON);
400+
_notificationActionTakenCallback(actionIdentifier, userInfoJSON, inputText);
375401
}
376402

377403
userInfoJSON.foreground = true;
@@ -500,9 +526,9 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
500526
return <UNUserNotificationCenterDelegateImpl>super.new();
501527
}
502528

503-
private callback: (unnotification: UNNotification, actionIdentifier?: string) => void;
529+
private callback: (unnotification: UNNotification, actionIdentifier?: string, inputText?: string) => void;
504530

505-
public initWithCallback(callback: (unnotification: UNNotification, actionIdentifier?: string) => void): UNUserNotificationCenterDelegateImpl {
531+
public initWithCallback(callback: (unnotification: UNNotification, actionIdentifier?: string, inputText?: string) => void): UNUserNotificationCenterDelegateImpl {
506532
this.callback = callback;
507533
return this;
508534
}
@@ -524,9 +550,10 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
524550
}
525551

526552
public userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler(center: UNUserNotificationCenter, response: UNNotificationResponse, completionHandler: () => void): void {
527-
console.log(">>>>>>>>>>> Handle push");
553+
console.log("Notification action response");
528554
console.log(response);
529-
this.callback(response.notification, response.actionIdentifier);
555+
556+
this.callback(response.notification, response.actionIdentifier, (<UNTextInputNotificationResponse>response).userText);
530557
completionHandler();
531558
}
532559
}

0 commit comments

Comments
 (0)