Tôi đã theo dõi this hướng dẫn dành cho nhà phát triển và yêu cầu Geofencing hoạt động trong ứng dụng của tôi như mong đợi.Android, Hiển thị alertDialog thay vì thông báo khi ứng dụng đang mở
Thông báo được gửi khi một Geofence chuyển tiếp xảy ra, từ bên trong một IntentService
:
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
...
sendNotification(geofenceTransitionDetails);
}
private void sendNotification(String notificationDetails) {
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Define the notification settings.
builder.setSmallIcon(R.mipmap.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText("Return to app")
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
Đây là cookie-cutter từ hướng dẫn. Mục đích được thiết lập trong Hoạt động chính:
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Làm cách nào để thêm chức năng chặn thông báo nếu ứng dụng đang mở và thay vào đó sẽ hiển thị AlertDialog cho người dùng? Lý tưởng nhất, tôi muốn có thể thực hiện các nhiệm vụ khác nhau, tùy thuộc vào chế độ xem mà người dùng hiện đang ở khi quá trình chuyển đổi địa lý diễn ra. Tôi có thể theo dõi/chặn chuyển đổi từ bên trong mỗi chế độ xem hoặc bằng cách nào đó trên toàn cầu không?
Xin cảm ơn trước.
Tôi vừa đăng nhập để nói lời cảm ơn. –