【问题标题】:Android Studio, receive notification on date if close to locationAndroid Studio,如果靠近位置,则会收到日期通知
【发布时间】:2016-06-08 10:17:17
【问题描述】:

大家好,我必须在某个date 中获得一个notification,如果该应用的用户靠近某个事件。

我正在考虑使用 push notification,但我必须知道如何在我的应用中实现。

你有一些代码示例给我看吗?谢谢

我从 google 阅读了有关 GCM 以更轻松地发送通知的信息?它是如何工作的?

【问题讨论】:

  • 在附近使用地理围栏。这是链接:developer.android.com/training/location/geofencing.html,关于日期我也在实施相同的概念,如果你在日期上成功,那么请告诉我..
  • 您想显示什么样的通知?您想从服务器获取一些数据以将其显示为通知还是仅基于日期和当前位置?

标签: java android xml notifications


【解决方案1】:

这是关于位置通知的小演示..

    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

【讨论】:

  • 不用担心..我也会为您提供我的应用程序链接.. bcz 它即将完成。这样您就可以全面了解此应用的工作流程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 2013-03-09
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
相关资源
最近更新 更多