Android local notification

Developer
Size
3,171 Kb
Views
24,288

How do I make an android local notification?

What is a android local notification? How do you make a android local notification? This script and codes were developed by Heedoo on 29 August 2022, Monday.

Android local notification Previews

Android local notification - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>android local notification</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Android local notification - Script Codes JS Codes

var Q = require('q');
Ti.App.Properties.setString("arrived", false);
/** * gets the current location of the user * @returns {Promise} */
function getCurrentLocation() {	var deferred = Q.defer();	if (!Ti.Geolocation.getLocationServicesEnabled()) {	alert('Location Services are not enabled');	deferred.reject({	location : null,	message : 'Location Services are not enabled'	});	} else {	Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH;	//ACCURACY_HIGH;	Ti.Geolocation.distanceFilter = 10;	//Ti.Geolocation.trackSignificantLocationChange = true;	Ti.Geolocation.addEventListener('location', function listener(_location) {	// remove event handler since event was received	Ti.Geolocation.removeEventListener('location', listener);	// process the results	if (!_location.error && _location && _location.coords) {	deferred.resolve({	location : _location,	message : null	});	} else {	alert('Location Services Error: ' + _location.error);	deferred.reject({	location : null,	message : _location.error	});	}	});	}	return deferred.promise;
};
/* fire get current location function */
getCurrentLocation().then(function(location) {	console.log("res: " + JSON.stringify(location));	if(!Ti.App.Properties.getString("arrived")){	checkArriveLocation(location);	}else{	checkDischargeLocation(location);	}
}, function(err) {	console.log("err:" + JSON.stringify(err));
});
/* * when user arrived hospital, check user is one mile away from hospital or not. */
function checkArriveLocation(e) {	Ti.App.Properties.setString("currentLat", e.location.coords.latitude);	Ti.App.Properties.setString("currentLng", e.location.coords.longitude);	var oneMile = getDistance(Ti.App.Properties.getString("currentLat"), Ti.App.Properties.getString("currentLng"), Ti.App.Properties.getString("hospitalLat"), Ti.App.Properties.getString("hospitalLng"));	if (oneMile < 3220000) {//1610000 <-- 1 mile	//arrived hospital	Ti.App.Properties.setString("arrived", true);	console.log("arrived hospital");	} else {	console.log("currently, more than one mile from hospital");	}
}
/* * if user is one mile away from hospital, send local notification. */
function checkDischargeLocation(e) {	Ti.App.Properties.setString("currentLat", e.location.coords.latitude);	Ti.App.Properties.setString("currentLng", e.location.coords.longitude);	var oneMile = getDistance(Ti.App.Properties.getString("currentLat"), Ti.App.Properties.getString("currentLng"), Ti.App.Properties.getString("hospitalLat"), Ti.App.Properties.getString("hospitalLng"));	if (oneMile > 1610000) {	//send local notification	console.log("send local notification");	//TODO need to change below time~	//setTimeout(function() {	sendLocalNotification();	//}, 10000);	//900000<-- every 15min	} else {	console.log("do not send local notification");	}
}
/* * send android local notification * http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android.Notification */
function sendLocalNotification() {	// Intent object to launch the application	var intent = Ti.Android.createIntent({	action : Ti.Android.ACTION_MAIN,	className : 'com.tellmydocs.TellMyDocs.TellmydocsActivity',	packageName : 'com.tellmydocs.TellMyDocs'	});	intent.flags |= Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK;	intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);	// Create a PendingIntent to tie together the Activity and Intent	var pending = Ti.Android.createPendingIntent({	activity: Ti.Android.currentActivity,	intent : intent,	type: Ti.Android.PENDING_INTENT_FOR_ACTIVITY,	flags : Ti.Android.FLAG_UPDATE_CURRENT	});	// Create the notification	var notification = Ti.Android.createNotification({	// icon is passed as an Android resource ID -- see Ti.App.Android.R.	contentTitle : 'TellMyDocs',	contentText : 'Have you been discharged from the hospital?',	contentIntent : pending,//Ti.Android.createPendingIntent({intent: Ti.Android.createIntent({})}),//pending,//NotificationClickAction,//Ti.Android.createIntent({}), //pending,	icon : Ti.App.Android.R.drawable.warn,	when : new Date(),	number : 1,	defaults:Titanium.Android.NotificationManager.DEFAULT_SOUND,	});	// Send the notification.	Ti.Android.NotificationManager.notify(1, notification);	// Remove all notifications sent by the application	//Titanium.Android.NotificationManager.cancelAll();	// Stop the service once the notification is sent	//Ti.Android.stopService(1, Ti.Android.currentService);//Ti.Android.currentService or Ti.Android.currentService.getIntent()	//TODO : received event (show alert)	//TODO : make stop when user mile away from hospital	//showAlert();
}
/* * get distance between two locations. * this function returns milimeter value * ex) 1610000 == 1 mile */
function getDistance(lat1, lon1, lat2, lon2) {	var R = 6371000000;	// m (change this constant to get miles)	var dLat = (lat2 - lat1) * Math.PI / 180;	var dLon = (lon2 - lon1) * Math.PI / 180;	var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));	var d = R * c;	return Math.round(d);
};
Android local notification - Script Codes
Android local notification - Script Codes
Home Page Home
Developer Heedoo
Username heedoo
Uploaded August 29, 2022
Rating 3
Size 3,171 Kb
Views 24,288
Do you need developer help for Android local notification?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Heedoo (heedoo) Script Codes
Create amazing sales emails with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!