メインコンテンツまでスキップ

Snapshot Notification

A newer version of the Maps SDK is available
This page uses v9.7.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.7.0, in the Maps SDK documentation.
Note

This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res directory. For example, see res/values/activity_strings.xml for R.string.* references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.

activity_snapshot_notification
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".examples.snapshot.SnapshotNotificationActivity">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="37.431"
mapbox:mapbox_cameraTargetLng="-120.981"
mapbox:mapbox_cameraZoom="4"/>

<TextView
android:id="@+id/notification_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="20dp"
android:text="@string/tap_on_map_for_notification"
android:textColor="@color/mapboxWhite"/>
</FrameLayout>
SnapshotNotificationActivity.java
package com.mapbox.mapboxandroiddemo.examples.snapshot;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;

import com.mapbox.mapboxandroiddemo.MainActivity;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshot;
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import static android.app.PendingIntent.getActivity;

/**
* Test activity showing how to use a the {@link com.mapbox.mapboxsdk.snapshotter.MapSnapshotter}
* in a way that utilizes provided bitmaps in native notifications.
*/
public class SnapshotNotificationActivity extends AppCompatActivity implements OnMapReadyCallback,
MapboxMap.OnMapClickListener {

private MapView mapView;
private MapSnapshotter mapSnapshotter;
private MapboxMap mapboxMap;
private NotificationManager notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_snapshot_notification);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

// Set a callback for when MapboxMap is ready to be used
mapView.getMapAsync(this);
}

@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
SnapshotNotificationActivity.this.mapboxMap = mapboxMap;
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
mapboxMap.addOnMapClickListener(SnapshotNotificationActivity.this);
}
});
}

@Override
public boolean onMapClick(@NonNull LatLng point) {
startSnapShot(
mapboxMap.getProjection().getVisibleRegion().latLngBounds,
mapView.getMeasuredHeight(),
mapView.getMeasuredWidth());
return true;
}

/**
* Creates bitmap from given parameters, and creates a notification with that bitmap
*
* @param latLngBounds of map
* @param height of map
* @param width of map
*/
private void startSnapShot(LatLngBounds latLngBounds, int height, int width) {
mapboxMap.getStyle(new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
if (mapSnapshotter == null) {
// Initialize snapshotter with map dimensions and given bounds
MapSnapshotter.Options options =
new MapSnapshotter.Options(width, height)
.withCameraPosition(mapboxMap.getCameraPosition())
.withStyle(style.getUri())
.withRegion(latLngBounds);

mapSnapshotter = new MapSnapshotter(SnapshotNotificationActivity.this, options);
} else {
// Reuse pre-existing MapSnapshotter instance
mapSnapshotter.setSize(width, height);
mapSnapshotter.setRegion(latLngBounds);
}
mapSnapshotter.start(new MapSnapshotter.SnapshotReadyCallback() {
@Override
public void onSnapshotReady(MapSnapshot snapshot) {
createNotification(snapshot.getBitmap());
}
});
}
});
}

/**
* Creates a notification with given bitmap as a large icon
*
* @param bitmap to set as large icon
*/
private void createNotification(Bitmap bitmap) {
final int notifyId = 1002;
String id = "channel_id";
if (notificationManager == null) {
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (notificationManager != null) {
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(id);
if (notificationChannel == null) {
notificationChannel = new NotificationChannel(id, "channel_name", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("channel_description");
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
Intent intent = new Intent(this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, id)
.setContentTitle("content")
.setSmallIcon(R.drawable.ic_circle)
.setContentTitle(getString(R.string.activity_image_generator_snapshot_notification_title))
.setContentText(getString(R.string.activity_image_generator_snapshot_notification_description))
.setContentIntent(getActivity(this, 0, intent, 0))
.setLargeIcon(bitmap);
Notification notification = builder.build();
notificationManager.notify(notifyId, notification);
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();

// Make sure to stop the snapshotter on pause if it exists
if (mapSnapshotter != null) {
mapSnapshotter.cancel();
}
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mapboxMap != null) {
mapboxMap.removeOnMapClickListener(this);
}
mapView.onDestroy();
}
}