Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions onebusaway-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ android {
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "\"https://onebusaway.co\""
buildConfigField "String", "FIXED_REGION_PLAUSIBLE_ANALYTICS_SERVER_URL", "null"
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}

/**
Expand Down Expand Up @@ -164,7 +163,6 @@ android {
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "null"
buildConfigField "String", "FIXED_REGION_PLAUSIBLE_ANALYTICS_SERVER_URL", "null"
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}

agencyY {
Expand Down Expand Up @@ -209,7 +207,6 @@ android {
buildConfigField "boolean", "FIXED_REGION_ENROLL_PARTICIPANTS_IN_STUDY", "false"
buildConfigField "String", "FIXED_REGION_SIDECAR_BASE_URL", "null"
buildConfigField "String", "FIXED_REGION_PLAUSIBLE_ANALYTICS_SERVER_URL", "null"
buildConfigField "String", "ONESIGNAL_APP_ID", ONESIGNAL_APP_ID
}
/**
* Add more rebranding flavors here...
Expand Down Expand Up @@ -348,8 +345,6 @@ dependencies {
implementation 'com.google.firebase:firebase-crashlytics-buildtools:3.0.2'
// Google Play Services Location
implementation 'com.google.android.gms:play-services-location:21.3.0'
// OneSignal
implementation 'com.onesignal:OneSignal:[5.0.0, 5.99.99]'
// Support libraries
implementation 'androidx.activity:activity-ktx:1.9.3'
implementation 'androidx.fragment:fragment-ktx:1.8.4'
Expand Down Expand Up @@ -378,6 +373,7 @@ dependencies {
// Pelias for point-of-interest search and geocoding for trip planning origin and destination
implementation 'com.github.OneBusAway:pelias-client-library:v1.1.0'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.firebase:firebase-messaging:24.1.0'
// Google Play Services Maps (only for Google flavor)
googleImplementation 'com.google.android.gms:play-services-maps:19.0.0'
// Google Play Services Places is required by ProprietaryMapHelpV2 (only for Google flavor)
Expand Down
13 changes: 13 additions & 0 deletions onebusaway-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@

<!-- Opt-out of June 2018 Android Maps styling with pin POI icons - see #891 -->
<meta-data android:name="com.google.android.gms.maps.API_OPTIONS" android:value="B3H9HE845CFHYG"/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/theme_primary" />
<service
android:name=".tripservice.MyFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>

<uses-feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.FirebaseMessaging;

import com.onebusaway.plausible.android.Plausible;
import com.onesignal.OneSignal;

import org.onebusaway.android.BuildConfig;
import org.onebusaway.android.R;
Expand All @@ -55,7 +55,6 @@
import org.onebusaway.android.util.BuildFlavorUtils;
import org.onebusaway.android.util.LocationUtils;
import org.onebusaway.android.util.PreferenceUtils;
import org.onebusaway.android.util.ReminderUtils;
import org.onebusaway.android.widealerts.GtfsAlerts;

import java.net.URI;
Expand Down Expand Up @@ -126,7 +125,7 @@ public void onCreate() {

incrementAppLaunchCount();

initOneSignal();
initFirebaseMessaging();

mDonationsManager = new DonationsManager(mPrefs, mFirebaseAnalytics, getResources(), getAppLaunchCount());

Expand Down Expand Up @@ -683,17 +682,22 @@ public static Boolean isIgnoringBatteryOptimizations(Context applicationContext)
return false;
}

private void initOneSignal() {
OneSignal.initWithContext(this, BuildConfig.ONESIGNAL_APP_ID);

// Handle click on the notification
OneSignal.getNotifications().addClickListener(iNotificationClickEvent -> {
ReminderUtils.openStopInfo(getBaseContext(), iNotificationClickEvent.getNotification());
});
private void initFirebaseMessaging() {
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
}
String token = task.getResult();
PreferenceUtils.saveString(getString(R.string.firebase_messaging_token), token);
});
}

public static String getUserPushNotificationID() {
return OneSignal.getUser().getPushSubscription().getId();
public static String getUserPushID() {
SharedPreferences preferences = getPrefs();
return preferences.getString(get().
getApplicationContext().getString(R.string.firebase_messaging_token), "");
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.onebusaway.android.tripservice

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.json.JSONException
import org.json.JSONObject
import org.onebusaway.android.R
import org.onebusaway.android.app.Application
import org.onebusaway.android.ui.ArrivalsListActivity
import org.onebusaway.android.util.PreferenceUtils
import org.onebusaway.android.util.ReminderUtils

/**
* Service class for handling Firebase Cloud Messaging (FCM) messages used for arrival reminders.
* This class extends `FirebaseMessagingService` to receive and handle FCM messages.
*/

class MyFirebaseMessagingService : FirebaseMessagingService() {

override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d("FirebaseMsgService", "Received notification")
}

override fun onNewToken(token: String) {
super.onNewToken(token)
PreferenceUtils.saveString(getString(R.string.firebase_messaging_token), token)
Log.d("FirebaseMsgService", "New token: $token")
}

private fun showNotification(message: String, stopId: String, notificationID: Int = 0) {
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val context = Application.get().applicationContext

val intent = ArrivalsListActivity.Builder(context, stopId).intent
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
val pendingIntent =
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

val notificationBuilder =
NotificationCompat.Builder(this, Application.CHANNEL_ARRIVAL_REMINDERS_ID)
.setSmallIcon(R.drawable.ic_stat_notification).setColor(Color.parseColor("#4CAF50"))
.setContentText(message).setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true).setChannelId(Application.CHANNEL_ARRIVAL_REMINDERS_ID)
.setSound(defaultSoundUri).setContentIntent(pendingIntent)

val appPrefs = Application.getPrefs()

val vibratePreference = appPrefs.getBoolean("preference_vibrate_allowed", true)
if (vibratePreference) {
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE)
}

val soundPreference = appPrefs.getString("preference_notification_sound", "")
if (soundPreference!!.isEmpty()) {
notificationBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
} else {
notificationBuilder.setSound(Uri.parse(soundPreference))
}

val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(
Application.CHANNEL_ARRIVAL_REMINDERS_ID, "Reminders", importance
).apply {
description = "Notification for arrival reminders"
setShowBadge(true)
}
notificationManager.createNotificationChannel(channel)
}

notificationManager.notify(notificationID, notificationBuilder.build())
}

override fun handleIntent(intent: Intent?) {
val data = intent?.extras as Bundle
val remoteMessage = RemoteMessage(data)
remoteMessage.data["payload"]?.let { jsonString ->
try {
val jsonObject = JSONObject(jsonString)
val dataObject = jsonObject.getJSONObject("data")

val message = dataObject.optString("body", "No message content")
val notificationID = dataObject.optInt("notification_id", 0)
val arrivalAndDepartureData = dataObject.getJSONObject("arrival_and_departure")

val tripId = arrivalAndDepartureData.optString("trip_id", "")
val stopId = arrivalAndDepartureData.optString("stop_id", "")
Log.d("FirebaseMsgService", "Received notification: $message and stopId: $stopId and tripId: $tripId")

ReminderUtils.deleteSavedReminder(
Application.get().applicationContext, tripId, stopId
)

showNotification(message, stopId, notificationID)
} catch (e: JSONException) {
Log.e("FirebaseMsgService", "Error parsing notification JSON: ${e.message}", e)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.messaging.FirebaseMessaging;

import com.sothree.slidinguppanel.SlidingUpPanelLayout;

Expand Down
Loading