Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Last active September 3, 2021 22:36
Show Gist options
  • Save lbrenman/d99076889cf55564af8bf2fd399af025 to your computer and use it in GitHub Desktop.
Save lbrenman/d99076889cf55564af8bf2fd399af025 to your computer and use it in GitHub Desktop.
Titanium Local Notifications Example
  1. Since we are using Local Notifications, we'll need to follow instructions here: http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Local_Notifications http://docs.appcelerator.com/platform/latest/#!/guide/Android_Notifications

  2. For iOS8 and later, we need to register to use local notifications using:

// Check if the device is running iOS 8 or later, before registering for local notifications
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
    Ti.App.iOS.registerUserNotificationSettings({
	    types: [
            Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
            Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
        ]
    });
}

When the code runs on iOS8 or later, the user will get the following warning:

  1. The following code creates and sends a local notification. Clicking on the notification will cause the app to foreground or start (if not already running)
$.index.open();

var alertFields = {
	title: 'Notification', //Android Only
	body : 'Just another notification',
	badge: 1,
	when: new Date(new Date().getTime() + 3000) //iOS only
};

if(OS_IOS){

	// Check if the device is running iOS 8 or later, before registering for local notifications
	if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
	    Ti.App.iOS.registerUserNotificationSettings({
		    types: [
	            Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
	            Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
	            Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
	        ]
	    });
	}

	// http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Local_Notifications
	// The following code snippet schedules an alert to be sent within three seconds
	var notification = Ti.App.iOS.scheduleLocalNotification({
	    alertBody: alertFields.body,
	    badge: alertFields.badge,
	    date: alertFields.when
	});
} else if (OS_ANDROID) {
	var notification = Titanium.Android.createNotification({
	    contentTitle: alertFields.title,
	    contentText : alertFields.body,
	    contentIntent: Ti.Android.createPendingIntent({intent: Ti.Android.createIntent({})}),
	    number: alertFields.badge
	});

	var intent = Ti.Android.createIntent({
	    action: Ti.Android.ACTION_MAIN,
	    className: 'com.helloworldlb.HelloworldlbActivity',
	    packageName: 'com.helloworldlb'
	});

	intent.flags |= Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED; //Starts app if not backgrounded
	intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);

	notification.contentIntent = Ti.Android.createPendingIntent({
		intent: intent,
		type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
		flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
	});

	Ti.Android.NotificationManager.notify(1, notification);

} else {
	alert('unsupported device');
}

Here is the alert on iOS (9.3):

Here is the alert on Android (5.0):

function doClick(e) {
alert($.label.text);
}
$.index.open();
var alertFields = {
title: 'Notification', //Android Only
body : 'Just another notification',
badge: 1,
when: new Date(new Date().getTime() + 3000) //iOS only
};
if(OS_IOS){
// Check if the device is running iOS 8 or later, before registering for local notifications
if (Ti.Platform.name == "iPhone OS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {
Ti.App.iOS.registerUserNotificationSettings({
types: [
Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
]
});
}
// http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Local_Notifications
// The following code snippet schedules an alert to be sent within three seconds
var notification = Ti.App.iOS.scheduleLocalNotification({
alertBody: alertFields.body,
badge: alertFields.badge,
date: alertFields.when
});
} else if (OS_ANDROID) {
var notification = Titanium.Android.createNotification({
contentTitle: alertFields.title,
contentText : alertFields.body,
contentIntent: Ti.Android.createPendingIntent({intent: Ti.Android.createIntent({})}),
number: alertFields.badge
});
var intent = Ti.Android.createIntent({
action: Ti.Android.ACTION_MAIN,
className: 'com.helloworldlb.HelloworldlbActivity',
packageName: 'com.helloworldlb'
});
intent.flags |= Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED; //Starts app if not backgrounded
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
notification.contentIntent = Ti.Android.createPendingIntent({
intent: intent,
type : Ti.Android.PENDING_INTENT_FOR_ACTIVITY,
flags : Ti.Android.FLAG_ACTIVITY_NO_HISTORY
});
Ti.Android.NotificationManager.notify(1, notification);
} else {
alert('unsupported device');
}
@qasim90
Copy link

qasim90 commented Mar 13, 2019

Would be good if this example also cover notification click handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment