Skip to content

Commit

Permalink
fix notifications for API>26
Browse files Browse the repository at this point in the history
  • Loading branch information
vitas committed Oct 28, 2018
1 parent 00c095f commit 40fc8e2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "28.0.1"
buildToolsVersion "28.0.3"
defaultConfig {
applicationId 'com.samebits.beacon.locator'
minSdkVersion 18
targetSdkVersion 26
versionCode 120
versionName '1.2.0'
targetSdkVersion 27
versionCode 121
versionName '1.2.1'
testApplicationId 'com.samebits.beacon.locator.test'
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void createNotification(Context context, String title, String msgText, S
PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainNavigationActivity.class), 0);

NotificationBuilder notificationBuilder = new NotificationBuilder(context);
notificationBuilder.createNotification(R.mipmap.ic_launcher, title, notificationIntent);
notificationBuilder.createNotification(R.mipmap.ic_launcher, title, isVibrate, notificationIntent);

notificationBuilder.setMessage(msgText);
notificationBuilder.setTicker(msgAlert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@

package com.samebits.beacon.locator.receiver;

import android.Manifest;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.samebits.beacon.locator.R;
import com.samebits.beacon.locator.util.Constants;
import com.samebits.beacon.locator.util.NotificationBuilder;


Expand All @@ -39,13 +44,19 @@ public class LocationReceiver extends BroadcastReceiver {
private int retryCount = 0;

@Override
public void onReceive(Context context, Intent intent)
{
public void onReceive(Context context, Intent intent) {
retryCount = intent.getIntExtra("RETRY_COUNT", 0);
Location bestLocation = null;
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
for (final String provider : locationManager.getAllProviders()) {
if (provider != null) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

Log.w(Constants.TAG, "No permissions to use GPS ");

return;
}
final Location location = locationManager.getLastKnownLocation(provider);
final long now = System.currentTimeMillis();
if (location != null
Expand All @@ -64,7 +75,7 @@ public void onReceive(Context context, Intent intent)
PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, mapIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationBuilder notificationBuilder = new NotificationBuilder(context);
notificationBuilder.createNotification(R.mipmap.ic_launcher, context.getString(R.string.action_alarm_text_title), notificationIntent);
notificationBuilder.createNotification(R.mipmap.ic_launcher, context.getString(R.string.action_alarm_text_title), true, notificationIntent);
notificationBuilder.setMessage(context.getString(R.string.notification_display_last_position));
notificationBuilder.show(1);

Expand All @@ -77,9 +88,15 @@ public void onReceive(Context context, Intent intent)
retryCount++;
intent.putExtra("RETRY_COUNT", retryCount);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

Log.w(Constants.TAG, "No permissions to use GPS ");

return;
}
locationManager.requestSingleUpdate(criteria, pendingIntent);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.samebits.beacon.locator.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand Down Expand Up @@ -52,9 +53,34 @@ public NotificationBuilder(Context mContext) {
/**
* Creation of notification on operations completed
*/
public NotificationBuilder createNotification(int smallIcon, String title, PendingIntent notifyIntent) {
mBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(smallIcon).setContentTitle(title)
.setAutoCancel(true).setColor(ContextCompat.getColor(mContext, R.color.hn_orange));
public NotificationBuilder createNotification(int smallIcon, String title, boolean isVibrate, PendingIntent notifyIntent) {

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "blocator_channel_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
title, NotificationManager.IMPORTANCE_HIGH);

// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
if (isVibrate) {
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
}
notificationManager.createNotificationChannel(notificationChannel);
}

mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);

mBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setColor(ContextCompat.getColor(mContext, R.color.hn_orange));

if (notifyIntent != null) {
mBuilder.setContentIntent(notifyIntent);
}
Expand Down

0 comments on commit 40fc8e2

Please sign in to comment.