Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Last active February 12, 2019 04:06
Show Gist options
  • Save hansemannn/0d56c66b2431757f3684e09553710108 to your computer and use it in GitHub Desktop.
Save hansemannn/0d56c66b2431757f3684e09553710108 to your computer and use it in GitHub Desktop.
Use the iOS 12 UserNotifications API's (threadIdentifier, summaryArgument, summaryArgumentCount) in Titanium!
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var btn = Ti.UI.createButton({
title: 'Schedule Notification'
});
btn.addEventListener('click', function() {
schedule('id_1', 'New Notification', 'Hey there!', new Date().getTime() + 3000);
schedule('id_2', 'Another Notification', 'Aloooah!', new Date().getTime() + 4000);
schedule('id_3', 'Anooooother Notification', 'Hola!', new Date().getTime() + 5000);
schedule('id_4', 'One more okay?', 'Hello!', new Date().getTime() + 6000);
schedule('id_5', 'This is the last one', 'Hallo!', new Date().getTime() + 7000);
});
win.add(btn);
win.open();
var acceptAction = Ti.App.iOS.createUserNotificationAction({
identifier: 'ACCEPT_IDENTIFIER',
title: 'Accept',
activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_FOREGROUND,
destructive: false,
authenticationRequired: true
});
// An action that does not open the app (background action)
var rejectAction = Ti.App.iOS.createUserNotificationAction({
identifier: 'REJECT_IDENTIFIER',
title: 'Reject',
activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
destructive: true,
authenticationRequired: false
});
// An action that does not open the app (background action), but lets the developer
// type in a text (iOS 9+)
var respondAction = Ti.App.iOS.createUserNotificationAction({
identifier: 'RESPOND_IDENTIFIER',
title: 'Respond',
activationMode: Ti.App.iOS.USER_NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
behavior: Ti.App.iOS.USER_NOTIFICATION_BEHAVIOR_TEXTINPUT, // or: Ti.App.iOS.USER_NOTIFICATION_BEHAVIOR_DEFAULT,
authenticationRequired: false
});
// Create an example notification category
var downloadContent = Ti.App.iOS.createUserNotificationCategory({
identifier: 'DOWNLOAD_CONTENT',
actionsForDefaultContext: [acceptAction, rejectAction, respondAction]
});
// Register for user notifications and categories
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT
],
categories: [downloadContent]
});
// Monitor notifications received while app is in the background
Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
alert('event: localnotificationaction');
});
// Validate notification settings
Ti.App.iOS.addEventListener('usernotificationsettings', function(e) {
Ti.API.info('event: usernotificationsettings');
})
// Monitor notifications received while app is in the foreground
Ti.App.iOS.addEventListener('notification', function(e) {
alert('event: notification');
});
function schedule(identifier, title, body, date) {
Ti.App.iOS.scheduleLocalNotification({
identifier: identifier,
alertTitle: title,
alertBody: body,
summaryArgument: 'My Summary',
summaryArgumentCount: 2,
threadIdentifier: 'my_thread',
date: date,
category: 'DOWNLOAD_CONTENT'
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment