Skip to content

Commit

Permalink
Add custom notification sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi755 committed Dec 19, 2024
1 parent 54de9aa commit 432d0a3
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 108 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".NavigationWithCustomForegroundNotificationActivity"
android:label="@string/title_custom_foreground_notification">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,94 +1,96 @@
package org.maplibre.navigation.android.example;

//import android.app.Notification;
//import android.app.NotificationChannel;
//import android.app.NotificationManager;
//import android.app.PendingIntent;
//import android.content.BroadcastReceiver;
//import android.content.Context;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.graphics.Color;
//import android.os.Build;
//
//import androidx.annotation.RequiresApi;
//import androidx.core.app.NotificationCompat;
//
//import org.maplibre.navigation.core.navigation.notification.NavigationNotification;
//import org.maplibre.navigation.core.routeprogress.RouteProgress;
//
//import static org.maplibre.navigation.core.navigation.NavigationConstants.NAVIGATION_NOTIFICATION_CHANNEL;
//
//public class CustomNavigationNotification implements NavigationNotification {
//
// private static final int CUSTOM_NOTIFICATION_ID = 91234821;
// private static final String STOP_NAVIGATION_ACTION = "stop_navigation_action";
//
// private final Notification customNotification;
// private final NotificationCompat.Builder customNotificationBuilder;
// private final NotificationManager notificationManager;
// private BroadcastReceiver stopNavigationReceiver;
// private int numberOfUpdates;
//
// public CustomNavigationNotification(Context applicationContext) {
// notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
//
// customNotificationBuilder = new NotificationCompat.Builder(applicationContext, NAVIGATION_NOTIFICATION_CHANNEL)
// .setSmallIcon(org.maplibre.navigation.android.navigation.ui.v5.R.drawable.ic_navigation)
// .setContentTitle("Custom Navigation Notification")
// .setContentText("Display your own content here!")
// .setContentIntent(createPendingStopIntent(applicationContext));
//
// customNotification = customNotificationBuilder.build();
// }
//
// @Override
// public Notification getNotification() {
// return customNotification;
// }
//
// @Override
// public int getNotificationId() {
// return CUSTOM_NOTIFICATION_ID;
// }
//
// @Override
// public void updateNotification(RouteProgress routeProgress) {
// // Update the builder with a new number of updates
// customNotificationBuilder.setContentText("Number of updates: " + numberOfUpdates++);
//
// notificationManager.notify(CUSTOM_NOTIFICATION_ID, customNotificationBuilder.build());
// }
//
// @Override
// public void onNavigationStopped(Context context) {
// context.unregisterReceiver(stopNavigationReceiver);
// notificationManager.cancel(CUSTOM_NOTIFICATION_ID);
// }
//
// public void register(BroadcastReceiver stopNavigationReceiver, Context applicationContext) {
// this.stopNavigationReceiver = stopNavigationReceiver;
//
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// applicationContext.registerReceiver(stopNavigationReceiver, new IntentFilter(STOP_NAVIGATION_ACTION), Context.RECEIVER_NOT_EXPORTED);
// } else {
// applicationContext.registerReceiver(stopNavigationReceiver, new IntentFilter(STOP_NAVIGATION_ACTION));
// }
// }
//
// private PendingIntent createPendingStopIntent(Context context) {
// Intent stopNavigationIntent = new Intent(STOP_NAVIGATION_ACTION);
// return PendingIntent.getBroadcast(context, 0, stopNavigationIntent, PendingIntent.FLAG_IMMUTABLE);
// }
//
// @RequiresApi(Build.VERSION_CODES.O)
// public void createNotificationChannel(Context context) {
// NotificationChannel chan = new NotificationChannel(NAVIGATION_NOTIFICATION_CHANNEL, "CustomNavigationNotification", NotificationManager.IMPORTANCE_NONE);
// chan.setLightColor(Color.BLUE);
// chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
// NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// if (service != null) {
// service.createNotificationChannel(chan);
// }
// }
//}
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import org.maplibre.navigation.android.navigation.ui.v5.notification.NavigationNotification;
import org.maplibre.navigation.core.routeprogress.RouteProgress;

import static org.maplibre.navigation.android.navigation.ui.v5.notification.MapLibreNavigationNotification.NAVIGATION_NOTIFICATION_CHANNEL;

public class CustomNavigationNotification implements NavigationNotification {

private static final int CUSTOM_NOTIFICATION_ID = 91234821;
private static final String STOP_NAVIGATION_ACTION = "stop_navigation_action";

private final Notification customNotification;
private final NotificationCompat.Builder customNotificationBuilder;
private final NotificationManager notificationManager;
private BroadcastReceiver stopNavigationReceiver;
private int numberOfUpdates;

public CustomNavigationNotification(Context applicationContext) {
notificationManager = (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);

customNotificationBuilder = new NotificationCompat.Builder(applicationContext, NAVIGATION_NOTIFICATION_CHANNEL)
.setSmallIcon(org.maplibre.navigation.android.navigation.ui.v5.R.drawable.ic_navigation)
.setContentTitle("Custom Navigation Notification")
.setContentText("Display your own content here!")
.setContentIntent(createPendingStopIntent(applicationContext));

customNotification = customNotificationBuilder.build();
}

@Override
public Notification getNotification() {
return customNotification;
}

@Override
public int getNotificationId() {
return CUSTOM_NOTIFICATION_ID;
}

@Override
public void updateNotification(RouteProgress routeProgress) {
// Update the builder with a new number of updates
customNotificationBuilder.setContentText("Number of updates: " + numberOfUpdates++);

notificationManager.notify(CUSTOM_NOTIFICATION_ID, customNotificationBuilder.build());
}

@Override
public void onNavigationStopped(Context context) {
context.unregisterReceiver(stopNavigationReceiver);
notificationManager.cancel(CUSTOM_NOTIFICATION_ID);
}

@SuppressLint("UnspecifiedRegisterReceiverFlag")
public void register(BroadcastReceiver stopNavigationReceiver, Context applicationContext) {
this.stopNavigationReceiver = stopNavigationReceiver;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
applicationContext.registerReceiver(stopNavigationReceiver, new IntentFilter(STOP_NAVIGATION_ACTION), Context.RECEIVER_NOT_EXPORTED);
} else {
applicationContext.registerReceiver(stopNavigationReceiver, new IntentFilter(STOP_NAVIGATION_ACTION));
}
}

private PendingIntent createPendingStopIntent(Context context) {
Intent stopNavigationIntent = new Intent(STOP_NAVIGATION_ACTION);
return PendingIntent.getBroadcast(context, 0, stopNavigationIntent, PendingIntent.FLAG_IMMUTABLE);
}

@RequiresApi(Build.VERSION_CODES.O)
public void createNotificationChannel(Context context) {
NotificationChannel chan = new NotificationChannel(NAVIGATION_NOTIFICATION_CHANNEL, "CustomNavigationNotification", NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (service != null) {
service.createNotificationChannel(chan);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected void onCreate(Bundle savedInstanceState) {
getString(R.string.description_foreground_notification),
NavigationWithForegroundNotificationActivity.class
));
list.add(new SampleItem(
getString(R.string.title_custom_foreground_notification),
getString(R.string.description_custom_foreground_notification),
NavigationWithForegroundNotificationActivity.class
));
RecyclerView.Adapter adapter = new MainAdapter(list);
recyclerView.setAdapter(adapter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,7 @@ class MockNavigationActivity :
getMapAsync(this@MockNavigationActivity)
}

val context = applicationContext
// val customNotification =
// CustomNavigationNotification(
// context
// )
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// customNotification.createNotificationChannel(this)
// }
val options = MapLibreNavigationOptions(
//// navigationNotification = customNotification
)
//
navigation = AndroidMapLibreNavigation(context, options)

navigation = AndroidMapLibreNavigation(applicationContext)
navigation.addMilestone(
RouteMilestone(
identifier = BEGIN_ROUTE_MILESTONE,
Expand All @@ -116,8 +103,7 @@ class MockNavigationActivity :
),
)
)
// customNotification.register(MyBroadcastReceiver(navigation), context)
//

binding.startRouteButton.setOnClickListener {
route?.let { route ->
binding.startRouteButton.visibility = View.INVISIBLE
Expand Down
Loading

0 comments on commit 432d0a3

Please sign in to comment.