Skip to content

Instantly share code, notes, and snippets.

@lbrenman

lbrenman/index.js

Last active Mar 13, 2019
Embed
What would you like to do?
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');
}
@Astrovic

This comment has been minimized.

Copy link

@Astrovic Astrovic commented Jun 15, 2017

Hi, Ti.Platform.name == "iPhone OS" will be always undefined for iPad and iPhone having iOS>10, because now Ti.Platform.name="iOS".
Then, you are already on if(OS_IOS){..., so you can use only if (parseInt(Ti.Platform.version.split(".")[0]) >= 8){... to check iOS>8

@mica83

This comment has been minimized.

Copy link

@mica83 mica83 commented Oct 11, 2017

Hi, with IOS i have a listner called "notification" for catch when user tap on notify banner. But with android? how catch when the user taps the notification banner?? I need a callback.

@MahmoudElmoghazy

This comment has been minimized.

Copy link

@MahmoudElmoghazy MahmoudElmoghazy commented Jan 10, 2018

@nikhes did u found any answer ?

@qasim90

This comment has been minimized.

Copy link

@qasim90 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
You can’t perform that action at this time.