Calling setProgress(0, 0, true) does not update Notification on > API 19 - android

Update:
After doing a lot more debugging, I figured out the cause of my notification not updating: Calling setProgress(0, 0, false) upon completion of the download. For some reason when this method is called before notify(), the upcoming update doesn't actually go through. Adding a NotificationChannel doesn't do anything.
My current workaround is to call setProgress(100, 100, false) so that the user can tell the download is finished and will subsequently get updated.
Original question:
I've got a custom file downloading utility that creates a notification whenever a file is being downloaded. This notification gets updated as the download progresses.
However, I am getting some odd results on API levels above 19. When the download is finished, the NotificationManager doesn't update the notification to inform the user of this. The notification does get updated whenever the download progresses, strangely enough.
Also, when I have the debugger active, the notification does get updated when the download completes. This leads me to believe some sort of race condition is happening here but I can't really seem to find out where or how.
My FileDownloader class:
public static void startGetRequestDownload(final Context context,
String fileUrl,
#Nullable String fileName,
#Nullable Header[] headers,
#Nullable RequestParams requestParams,
final boolean showProgressNotification){
final int notificationID = 0;
final NotificationManager mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(mNotifyManager == null) {
Log.e(TAG, "Failed to get NotificationManager service");
return;
}
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String notificationTitle = context.getString(R.string.default_filename);
if(fileName != null && !fileName.isEmpty()){
notificationTitle = fileName;
}
mBuilder.setContentTitle(notificationTitle)
.setContentText(context.getString(R.string.download_in_progress))
.setColor(ContextCompat.getColor(context, R.color.app_color_accent))
.setSmallIcon(R.drawable.ic_elab);
String uuid = "Temp_" + System.currentTimeMillis();
final File tempFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), uuid);
// Temporarily disable SSL as workaround.
RestClient.getClient().setSSLSocketFactory(MySSLSocketFactory.getFixedSocketFactory());
RestClient.getClient().get(context, fileUrl, headers, requestParams, new FileAsyncHttpResponseHandler(tempFile) {
#Override
public void onStart() {
super.onStart();
if(showProgressNotification){
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
#Override
public void onProgress(long bytesWritten, long totalSize) {
super.onProgress(bytesWritten, totalSize);
if(showProgressNotification){
int progress = (int) (bytesWritten / totalSize * 100);
mBuilder.setProgress(100, progress, false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
if(showProgressNotification){
mBuilder.setContentText(context.getString(R.string.download_failed));
mBuilder.setProgress(0,0,false);
mNotifyManager.notify(notificationID, mBuilder.build());
}
Log.w(TAG, "File download failed", throwable);
}
#Override
public void onSuccess(int statusCode, Header[] headers, File file) {
FileMetaData metaData = validateResponse(headers);
if(metaData != null){
final File downloadDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
File newFile = new File(downloadDirectory, metaData.fileName);
if(file.renameTo(newFile)){
Uri fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", newFile);
Intent actionViewIntent = new Intent(Intent.ACTION_VIEW);
actionViewIntent.setDataAndType(fileUri, metaData.contentType);
actionViewIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if(showProgressNotification){
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, actionViewIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setProgress(0,0,false);
mBuilder.setContentText(context.getString(R.string.download_completed));
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSmallIcon(android.R.drawable.stat_sys_download_done);
mNotifyManager.notify(notificationID, mBuilder.build());
}
return;
}
}
//Failover
if(showProgressNotification){
mBuilder.setContentText(context.getString(R.string.download_failed));
mNotifyManager.notify(notificationID, mBuilder.build());
}
}
});
}

I figured out the actual reason as to why my notification wasn't getting updated. The Android documentation mentions the following in regards to notification updates:
Caution: The system applies a rate limit to updating notifications. If you post updates to a notification too frequently, the system may drop some notifications.
I was calling notify() every time the download progressed. Obviously, the download progress method was called very often, which resulted in several notifications getting blocked. However, I only saw the last notification getting dropped: The download completion notification.
Limiting the amount of calls to notify() in onProgress() fixed my issue.

Related

Android Target API 31 best way to run a service in background that ALWAYS keep listening to my messaging server (RabbitMQ)

I am trying to create a notification service in my android app that always keeps listening to my RabbitMQ server for new messages. I want it to be able to send notifications even from background. Basically I am trying to create a notification communication between two client side application (App1 and App2) through Rabbit MQ and send notifications to both the apps in case of an event.
I have implemented it using JOB Service class but it is not consistent and it stops after sometime. Can someone please help me in understanding the architecture in a better way. How can I achieve something like Firebase Messaging Service but through Rabbit MQ?
Sample code that I have used below:
public class StoreOrderJobService extends JobService {
private static final String TAG = "JobService";
Random random = new Random();
SharedPrefManager prefManager;
private boolean jobCancelled = false;
#Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d(TAG, "Job Started");
prefManager = new SharedPrefManager(this);
subscribeStore(prefManager.getUserId(), jobParameters);
return true;
}
private void subscribeStore(String storeId, JobParameters parameters) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(HOST_IP);
factory.setAutomaticRecoveryEnabled(false);
String queueName = prefManager.getSessionId();
if (queueName != null) {
Thread subscribeStoreThread = new Thread(new Runnable() {
#Override
public void run() {
Log.d(TAG, "Job Started");
try {
if (jobCancelled) {
return;
}
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
Log.d("OrderService", "Session Id " + queueName);
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, "store_test", storeId);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
Log.d("OrderService", "Received message " + message);
Envelope envelope = delivery.getEnvelope();
String routingKey = envelope.getRoutingKey();
if (routingKey.equals(storeId)) {
channel.basicAck(envelope.getDeliveryTag(), true);
String message_new = new String(delivery.getBody(), "UTF-8");
Gson gson = new Gson();
OrderSubscribePayload payload = gson.fromJson(message_new, OrderSubscribePayload.class);
Log.d("order Service", "Order Id " + payload.getOrderId());
sendOrderNotification(random.nextInt(), payload);
}
};
channel.basicConsume(queueName, false, deliverCallback, consumerTag -> {
});
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
});
subscribeStoreThread.start();
}
}
private void sendOrderNotification(int id, OrderSubscribePayload payload) {
Log.d("Service", "sendOrderNotification " + payload.getOrderId());
Intent contextIntent = new Intent(this, OrderDetails.class);
Bundle args = new Bundle();
args.putSerializable("orderDetails", (Serializable) payload);
contextIntent.putExtra("Bundle", args);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent pIntent = PendingIntent.getActivity(this, iUniqueId, contextIntent, 0);
Notification n = new NotificationCompat.Builder(this, ManagedApplication.CHANNEL_ORDER_ID)
.setContentTitle("New Order")
.setContentText("Received New Order")
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setColor(getResources().getColor(R.color.color_primary))
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
notificationManager.notify(id, n);
}
#Override
public boolean onStopJob(JobParameters jobParameters) {
Log.d(TAG, "Job Cancelled");
jobCancelled = true;
return true;
}
}
I am calling this job on users login as below:
private void startNotificationJob() {
ComponentName componentName = new ComponentName(this, StoreOrderJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setPersisted(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(15 * 60 * 1000)
.build();
JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
int result = jobScheduler.schedule(info);
if (result == JobScheduler.RESULT_SUCCESS) {
Log.d("JOB Scheduler", "Job Scheduled");
} else Log.d("JOB Scheduler", "Job Scheduled Failed");
}
I have implemented it using JOB Service class but it is not consistent and it stops after sometime.
Nothing will be keeping your process running, which you will need for your desired functionality. Nothing will be keeping the CPU powered on all the time, which also will be needed for your desired functionality.
Please bear in mind that what you want is rather harmful to the battery life.
How can I achieve something like Firebase Messaging Service but through Rabbit MQ?
Use a foreground service, with a continuous partial WakeLock, in a separate process (android:process manifest attribute) than the UI of your app. You may also need code to deal with re-establishing your MQ connection as the device changes connectivity (e.g., from WiFi to mobile data) or loses and then regains connectivity.
You will also need to ask your users to go into the Settings app and try to opt your app out of all battery optimizations. Note that this will not be possible on all devices.
You will also need to ask your users to never kill your app's task. The separate process may help on some devices if users forget, but that behavior seems to vary by device.
And you will need to take into account that your solution will not be reliable, because some device manufacturers to prevent developers from doing the sorts of things that you want to do, as those things are bad for battery life.

Notification does not show immediately even provided by a unique id

I check it many times using Logs and prevent the notify() method of manager to be called multiple times if not necessary to avoid some overhead. Now I have first notification with a 0% progress then again I create a new notification with a 0% progress again, unfortunately only one notification is shown even their id is unique but later on when the first notification progress gets updated example from 0% to 25% then that's the only time it will show the desired output, a 2 notification with different progress value. I am using only one Notification, Notification Manager, and in Notification Builder since I do not want to create an overlap animation of notification when it gets updated. Is this expected behavior when in the foreground?
public abstract class BaseTaskService extends Service {
private static final String TAG = "BaseTaskService";
private static final String CHANNEL_ID_DEFAULT = "Upload and Download";
private int queue = 0;
private FirebaseFirestore mDatabase;
private final List<Integer> listOfTaskID = new ArrayList<>();
private final SparseIntArray totalUnitList = new SparseIntArray();
private final SparseIntArray completedUnitList = new SparseIntArray();
private Notification notification;
private NotificationManager notificationManager;
private final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID_DEFAULT);
public void taskStarted(int id, boolean isUpload) {
//Increase the number of task
changeNumberOfTasks(1);
//Check if the task is new or not, if new then start a foreground service using id for it and add it to the list
if (!listOfTaskID.contains(id)){
listOfTaskID.add(id);
startForeground(id, notification);
Log.d(TAG, "Foreground Task Created : ID = " + id);
}
//If called by Upload Service, start the service once as a foreground per post
//If called by Download Service, start the service once as a foreground per file
if (isUpload){
//Set a total unit of files per post since one post could incorporate numerous images or files
totalUnitList.append(id, totalUnitList.get(id, 0) + 1);
Log.d(TAG, "Total Units For " + id + ": (" + totalUnitList.get(id) + ")");
}
}
public void taskCompleted() {
changeNumberOfTasks(-1);
}
private synchronized void changeNumberOfTasks(int delta) {
//Update the queue by adding delta value which could be 1 or -1
//Queue will display the overall upload or download of file from different tasks
queue += delta;
Log.d(TAG, "Overall Number of Remaining Task: " + queue);
//If there are no tasks left in queue, stop the service :)
if (queue <= 0) {
Log.d(TAG, "Stopping...");
//In Upload Service if there is no task in our queue it means that all request was finished
//so we need to reset the list of post's total task and completed task to zero
totalUnitList.clear();
completedUnitList.clear();
//Clear all of the id task
listOfTaskID.clear();
//Stop the foreground and remove all notification
stopForeground(true);
//Stop this service, calling this method will dismiss the very recent notification.
stopSelf();
}
}
#Override
public void onCreate() {
super.onCreate();
mDatabase = FirebaseFirestore.getInstance();
if (!isNotificationChannelEnabled(CHANNEL_ID_DEFAULT))
Toast.makeText(this, "Please turn on the notification in the app settings.", Toast.LENGTH_SHORT).show();
}
/*
We could use this line but unfortunately it will no longer work on Android O and above so we'll use the hashcode below.
This line is suppose to use for separating/detaching the Foreground notification from a Service
so that generating a separated unique id for PendingIntent and Finished notification is no longer needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
stopForeground(STOP_FOREGROUND_DETACH);
else
ServiceCompat.stopForeground(this, STOP_FOREGROUND_DETACH);
*/
//For Android O and above
private void createDefaultChannel() {
// Since Android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(CHANNEL_ID_DEFAULT,
"Upload and Download",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
}
public boolean isNotificationChannelEnabled(String channelId){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if(channelId != null) {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(channelId);
return channel.getImportance() != NotificationManager.IMPORTANCE_NONE;
}
return false;
} else {
return NotificationManagerCompat.from(this).areNotificationsEnabled();
}
}
/**
* Show notification with a progress bar.
* Updating the progress happens here
* This is for DOWNLOAD SERVICE
*/
void showProgressNotification(String caption, long completedUnits, long totalUnits, int id) {
createDefaultChannel();
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Compute the progress
int percentComplete = 0;
if (totalUnits > 0) {
percentComplete = (int) (100 * completedUnits / totalUnits);
}
//To update and separate the notification progress according to its task
notification = notificationBuilder
.setProgress(100, percentComplete, false)
.setContentInfo(String.valueOf(percentComplete +"%"))
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(caption)
.setAutoCancel(false)
.setOngoing(true)
.build();
if (!listOfTaskID.contains(id))
Log.d(TAG, "Download Notification Created: ID = " + id);
else
Log.d(TAG, "Download Notification Updated: ID = " + id);
//Notify the manager that we have a new update with notification
notificationManager.notify(id, notification);
}
/**
* Show notification with a progress bar.
* Updating the progress happens here
* This is for UPLOAD SERVICE
*/
void showProgressNotification(String caption, final String path, final int id, boolean isComplete, String title, String desc) {
createDefaultChannel();
//If null then initialize the Notification Manager
if (notificationManager == null)
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Increment only if it is a successful task
if (isComplete)
completedUnitList.append(id, completedUnitList.get(id,0) + 1);
//Update and compute the progress
double percentComplete = 0;
if (totalUnitList.get(id, 0) > 0) {
//Perform this line if and only the total task is not equal to zero since dividing a number by zero is Error
percentComplete = (100 / totalUnitList.get(id)) * completedUnitList.get(id, 0);
}
notification = notificationBuilder
.setProgress(100, (int) percentComplete, false)
.setContentInfo(String.valueOf((int) percentComplete +"%"))
.setSmallIcon(R.drawable.ic_file_upload_white_24dp)
.setContentTitle(getString(R.string.app_name))
.setContentText(caption)
.setAutoCancel(false)
.setOngoing(true)
.build();
//This if condition is use to avoid repetitive call of notify() and will be triggered only if new task is created
if (!isComplete && !listOfTaskID.contains(id)){
Log.d(TAG, "Upload Notification Created: ID = " + id);
//Notify the manager that we have a new notification
notificationManager.notify(id, notification);
}
else if (isComplete){
Log.d(TAG, "Upload Notification Updated: ID = " + id);
//Notify the manager that we have a new update with notification
notificationManager.notify(id, notification);
//Check now if the number of completed task is equal to the number of total task if yes then show a finish notification
if (completedUnitList.get(id) == totalUnitList.get(id)){
Map<String, Object> details = new HashMap<>();
details.put(getResources().getString(R.string.Description), desc);
//We will use milliseconds to calculate how long is the post and for query
details.put(getResources().getString(R.string.Time_Posted), String.valueOf(new Date().getTime()));
details.put(getResources().getString(R.string.file), true);
if (title != null){
details.put(getResources().getString(R.string.Title),title);
details.put(getResources().getString(R.string.SU).toLowerCase(), Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
}
else
details.put(getResources().getString(R.string.uid), Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
//Make Intent to MainActivity
final Intent intent = new Intent(BaseTaskService.this, SUMain.class)
.putExtra(UploadService.DATA_COLLECTION, path)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mDatabase.document(path).set(details).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
showFinishedNotification(getString(R.string.upload_success), intent, true, id, true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
showFinishedNotification(getString(R.string.upload_failure), intent, false, id, true);
}
});
}
}
}
/**
* Show notification that the activity finished.
*/
void showFinishedNotification(String caption, Intent intent, boolean isSuccess, int id, boolean isUpload) {
createDefaultChannel();
//Since calling a stopSelf() method will kill the service itself and dismissed the very recent Finished notification which is wrong in our case.
//Create a new id for Finished notification that is not bounded from the id of the progress notification, service, and foreground.
String uri = isUpload ? String.valueOf(intent.getParcelableExtra(UploadService.FILE_URI)) : String.valueOf(intent.getParcelableExtra(DownloadService.DOWNLOAD_URI));
//Use the hashcode of current timestamp mixed with some string to make it unique.
int newID = (uri + System.currentTimeMillis()).hashCode();
//Make PendingIntent for notification with the new generated unique id
PendingIntent pendingIntent = PendingIntent.getActivity(this, newID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
int icon = isSuccess ? R.drawable.ic_done : R.drawable.ic_error_white_24dp;
notification = notificationBuilder
.setProgress(0, 0, false)
.setContentTitle(getString(R.string.app_name))
.setContentIntent(pendingIntent)
.setContentText(caption)
.setContentInfo(null)
.setAutoCancel(true)
.setSmallIcon(icon)
.setOngoing(false)
.build();
//Remove the first notification that has a incremental id which is the notification with progress
notificationManager.cancel(id);
//Show a new notification after removing the progress notification with the new generated unique id
notificationManager.notify(newID, notification);
Log.d(TAG, "Finished Notification: ID = " + newID);
}}

android.os.TransactionTooLargeException for ForegroundService notification

I'm writing an android app that tracks the user's location and shows the distance, time and price of the trip in a notification, all this tracked in a ForegroundService. The service tracks the location, price and the time, and I'm updating the notification every second.
I get this TransactionTooLargeException in production for the very first notification update, it only happens on Samsung devices running Android 8.0+.
This is what's happening:
I call the start method from the service:
public void start(MeasureService service) {
service.startForeground(NOTIFICATION_ID, getNotification(0, 0, 0));
}
I call the update method from the service:
public void update(int price, float meters, long time) {
mNotificationManager.notify(NOTIFICATION_ID, getNotification(price, meters, time));
}
Actually, these calls are called right after one another, the crash is coming from the update method. Can this (calling them one after the other) be a problem?
this is the getNotification:
private Notification getNotification(int price, float meters, long seconds) {
if (mNotificationBuilder == null) {
createNotificationBuilder();
}
return mNotificationBuilder
.setCustomContentView(getSmallView(price))
.setCustomBigContentView(getBigView(price, seconds, meters))
.build();
}
where the getSmallView and getBigView methods are like this:
private RemoteViews getSmallView(int price) {
String priceText = ...;
mSmallView.setTextViewText(R.id.price, priceText);
return mSmallView;
}
private RemoteViews getBigView(int price, long seconds, float meters) {
String priceText = ...;
String timeText = ...;
String distanceText = ...;
mBigView.setTextViewText(R.id.price, priceText);
mBigView.setTextViewText(R.id.time, timeText);
mBigView.setTextViewText(R.id.distance, distanceText);
return mBigView;
}
and this is how I create the notificationBuilder:
private void createNotificationBuilder() {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
mNotificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
mNotificationBuilder = mNotificationBuilder
.setSmallIcon(R.drawable.icon)
.setOngoing(true)
.setContentIntent(getOpenMeasurePageIntent());
}
and the getOpenMeasurePageIntent:
private PendingIntent getOpenMeasurePageIntent() {
Intent launchMeasureIntent = new Intent(mContext, MainActivity.class);
launchMeasureIntent.setAction(...);
launchMeasureIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
return PendingIntent.getActivity(mContext, 0, launchMeasureIntent, 0);
}
This is the crash log I get:
Caused by: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 560988 bytes
16 at android.app.NotificationManager.notifyAsUser(NotificationManager.java:319)
17 at android.app.NotificationManager.notify(NotificationManager.java:284)
18 at android.app.NotificationManager.notify(NotificationManager.java:268)
19 at com.myapp.service.measure.MyNotification.void update(int,float,long) <- this is called from the timer
I found a lot of similar issues online, but they are usually about passing big chunks of data in the intent, which I believe I'm not doing.
Any idea what I might be doing wrong?
i think problem is in updating Notification every seconds.
Solution
i suggest you should update notification only when data likes(distance, price) changed.

FCM background notification issue android

I am using FCM in my android project and is working fine when app is running. But when the app is killed or closed onReceiveMessage is not called. I have tried by sending message using only data playload also still it is not working. Is there any any solution. Thanks in advance
Try this code it will solve your problem. MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Title: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
sendNotification("", "");
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
private void sendNotification(String xyz, String abc) {
Intent intent = new Intent(this, MainActivity.class);
Bundle b = new Bundle();
b.putString("key", xyz);
b.putString("key", abc);
intent.putExtras(b);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("title")
.setContentText("body")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
boolean useSilhouette = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
if (useSilhouette) {
try {
notificationBuilder.setSmallIcon(R.mipmap.ic_sillate);
} catch (Exception e) {
notificationBuilder.setSmallIcon(R.mipmap.ic_sillate);
}
} else {
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Random id number for notification
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
notificationManager.notify(m/* ID of notification */, notificationBuilder.build());
}
}
Get values via getIntent() in MainActivity and do what you want to do ;)

Android location tracker crashes after a long time

I have created a location tracking app, which writes to a local SQLite database every time the location is changed.
The app unfortunately crashes after about 7-8 hours while tracking, unfortunately this does not happen when I have connected the device to a debugger, so there is no log which I can attach.
Some more maybe useful information:
The app crashes before it is waken up from the background(can see that clearly in the tracked data), so I can exclude this bug from other apps
Tried to write into textfiles instead of the database without any success(Just ran about 3 hours before crashing)
Changing tracking interval(5s normal 1s fastest interval): Same result app crashes also after 7-8 hours
Here are some code snippets:
Location change event
#Override
public void onLocationChanged(Location location) {
if(location == null){
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(location == null) {
return;
}
}
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
ActivitylogRepo activityRepo = new ActivitylogRepo(this);
Activitylog activity = new Activitylog();
activity.activity = "Position";
activity.timestamp = getDateTime();
activity.value2 = String.valueOf(currentLatitude);
activity.value3 = String.valueOf(currentLongitude);
activityRepo.insert(activity);
}
Database insert command
public int insert(Activitylog activitylog) {
//Open connection to write data
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Activitylog.KEY_activity, activitylog.activity);
values.put(Activitylog.KEY_timestamp, activitylog.timestamp);
values.put(Activitylog.KEY_value1, activitylog.value1);
values.put(Activitylog.KEY_value2, activitylog.value2);
values.put(Activitylog.KEY_value3, activitylog.value3);
values.put(Activitylog.KEY_value4, activitylog.value4);
// Inserting Row
long activitylog_id = db.insert(Activitylog.TABLE, null, values);
db.close(); // Closing database connection
return (int) activitylog_id;
}
Initializing service
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(20 * 1000) // 20s in ms
.setFastestInterval(5 * 1000); // 5s in ms
I have something that will help you to catch your crash report even when you're not attached to the debugger - thus helping you to get to the root of the problem! I've posted this as an answer so I can format it nicely.
The class below will handle uncaught exceptions, packing them up into an email and placing a notification on your phone (you can adjust this to whatever you need)
public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context mContext;
private java.lang.Thread.UncaughtExceptionHandler mDefaultUEH;
public UncaughtExceptionHandler(Context context, java.lang.Thread.UncaughtExceptionHandler defaultUEH) {
mContext = context;
mDefaultUEH = defaultUEH;
}
#Override
public void uncaughtException(Thread thread, Throwable ex) {
// Make error into something more readable
String timestamp = android.text.format.DateFormat.getLongDateFormat(mContext).format(
new Date(System.currentTimeMillis()));
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
// Create email intent for error
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_EMAIL, "email address");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Crash Report " + timestamp);
emailIntent.putExtra(Intent.EXTRA_TEXT, stacktrace);
// Make into pending intent for notifcation
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
Intent.createChooser(emailIntent, "Send report with.."),
Intent.FLAG_ACTIVITY_NEW_TASK);
// here create a notification for the user
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("Crash Caught");
builder.setContentText("Send to Developer");
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_notification);
// Finally display the notification!
NotificationManager mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1337, builder.build());
// re-throw critical exception further to the os (important)
mDefaultUEH.uncaughtException(thread, ex);
}
}
Set this up in your Application class like this:
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
/*
* Set up uncaught exception handler
*/
java.lang.Thread.UncaughtExceptionHandler defaultUEH =
Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new
UncaughtExceptionHandler(this, defaultUEH));
}
}
I don't recommend including this code in your release version! At that point, you can use the Developer Console to get crash reports.

Categories

Resources