-
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
-
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:
- 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):
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.