嗨如果應用的用戶接近特定事件,則必須在某個date
中獲得notification
。Android Studio,如果接近位置,會收到日期通知
我在想使用推notification
但我必須知道如何在我的應用程序中實現。
你有一些代碼示例來顯示我嗎?謝謝
我已經閱讀關於GCM從谷歌更容易發送通知?它是如何工作的?
嗨如果應用的用戶接近特定事件,則必須在某個date
中獲得notification
。Android Studio,如果接近位置,會收到日期通知
我在想使用推notification
但我必須知道如何在我的應用程序中實現。
你有一些代碼示例來顯示我嗎?謝謝
我已經閱讀關於GCM從谷歌更容易發送通知?它是如何工作的?
這裏是關於位置通知小演示..
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
我會嘗試,謝謝 – FreedomSka
不用擔心..我會爲您提供我的應用程序鏈接也bcz即將完成。所以你可以瞭解這個應用程序的工作流程。 –
使用地理圍欄附近的位置。這裏的鏈接:https://開頭開發商。 android.com/training/location/geofencing.html,以及有關日期即時通訊也實現相同的概念,如果你成功的日期,然後plz告訴我.. –
什麼樣的通知,你想顯示?你想從服務器獲取一些數據來顯示它爲通知還是僅基於日期和當前位置? –
在通知面板上的正常通知,就像來自whatsapp的消息,但告訴我,如果我在一個特定日期的城鎮 – FreedomSka