여기
public class AreWeThereIntentService extends IntentService {
private final String TAG = AreWeThereIntentService.class.getName();
private SharedPreferences prefs;
private Gson gson;
public AreWeThereIntentService() {
super("AreWeThereIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
prefs = getApplicationContext().getSharedPreferences(
Constants.SharedPrefs.Geofences, Context.MODE_PRIVATE);
gson = new Gson();
// 1. Get the event
GeofencingEvent event = GeofencingEvent.fromIntent(intent);
if (event != null) {
if (event.hasError()) {
onError(event.getErrorCode());
} else {
// 2. Get the transition type
int transition = event.getGeofenceTransition();
if (transition == Geofence.GEOFENCE_TRANSITION_ENTER ||
transition == Geofence.GEOFENCE_TRANSITION_DWELL ||
transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<String> geofenceIds = new ArrayList<>();
// 3. Accumulate a list of event geofences
for (Geofence geofence : event.getTriggeringGeofences()) {
geofenceIds.add(geofence.getRequestId());
}
if (transition == Geofence.GEOFENCE_TRANSITION_ENTER ||
transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
// 4. Pass the geofence list to the notification method
onEnteredGeofences(geofenceIds);
}
}
}
}
}
private void onEnteredGeofences(List<String> geofenceIds) {
// 1. Outer loop over all geofenceIds
for (String geofenceId : geofenceIds) {
String geofenceName = "";
// 2, Loop over all geofence keys in prefs and retrieve NamedGeofence from SharedPreferences
Map<String, ?> keys = prefs.getAll();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
String jsonString = prefs.getString(entry.getKey(), null);
NamedGeofence namedGeofence = gson.fromJson(jsonString, NamedGeofence.class);
if (namedGeofence.id.equals(geofenceId)) {
geofenceName = namedGeofence.name;
break;
}
}
// 3. Set the notification text and send the notification
String contextText =
String.format(this.getResources().getString(R.string.Notification_Text), geofenceName);
// 1. Create a NotificationManager
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MapsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AppSettings settings = AppSettings.getSettings(getApplication());
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.getResources().getString(R.string.Notification_Title))
.setContentText(contextText)
.setContentIntent(pendingNotificationIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(contextText))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.setVibrate(new long[0])
.setWhen(System.currentTimeMillis())
.build();
try {
Uri song = Uri.parse(settings.getSdcard());
if (song == null) {
notification.sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/battlefield");
} else {
notification.sound = Uri.parse(settings.getSdcard());
}
} catch (NullPointerException e) {
e.printStackTrace();
}
notificationManager.notify(0, notification);
}
}
private void onError(int i) {
Log.e(TAG, "Geofencing Error: " + i);
}
}
.. 위치 알림에 대한 작은 데모 나이 샘플 응용 프로그램 코드를 사용할 수 있습니다 근처에 위치 link
사용 지오 펜스를 .. 여기 링크의 : https : //로 개발. android.com/training/location/geofencing.html 및 날짜에 대해서도 동일한 개념을 구현합니다. 날짜가 성공하면 plz에게 알려주세요 .. –
표시 할 알림의 종류는 무엇입니까? 서버에서 일부 데이터를 가져 와서 알림으로 표시하겠습니까, 아니면 날짜와 현재 위치 만 기반으로합니까? –
알림 패널의 정상적인 알림, whatsapp의 메시지와 비슷하지만 특정 날짜의 마을에있는 경우 알려줍니다. – FreedomSka