Notifications improvements (#78)

* Refactor notifications
* Change grade notify icon
This commit is contained in:
Rafał Borcz 2018-04-08 16:37:27 +02:00 committed by Mikołaj Pich
parent 0aa8c5605d
commit 78d57ca746
17 changed files with 76 additions and 24 deletions

View File

@ -0,0 +1,36 @@
package io.github.wulkanowy.services;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import io.github.wulkanowy.R;
class GradeNotify extends NotificationService {
private static final String CHANNEL_ID = "Grade_Notify";
GradeNotify(Context context) {
super(context);
}
@Override
@TargetApi(26)
void createChannel() {
String channelName = getString(R.string.notify_grade_channel);
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName,
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
}
@Override
String getChannelId() {
return CHANNEL_ID;
}
}

View File

@ -3,29 +3,22 @@ package io.github.wulkanowy.services;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import java.util.Random;
class NotificationService {
private static final String CHANNEL_ID = "Wulkanowy_New_Grade_Channel";
private static final String CHANNEL_NAME = "New Grade Channel";
public class NotificationService {
private NotificationManager manager;
private Context context;
NotificationService(Context context) {
public NotificationService(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
}
void notify(Notification notification) {
@ -33,23 +26,32 @@ class NotificationService {
}
NotificationCompat.Builder notificationBuilder() {
return new NotificationCompat.Builder(context, CHANNEL_ID);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
return new NotificationCompat.Builder(context, getChannelId());
}
@TargetApi(26)
private void createChannel() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
public void cancelAll() {
getManager().cancelAll();
}
private NotificationManager getManager() {
NotificationManager getManager() {
if (manager == null) {
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
return manager;
}
}
String getString(@StringRes int stringId) {
return context.getString(stringId);
}
@TargetApi(26)
void createChannel() {
}
String getChannelId() {
return null;
}
}

View File

@ -81,12 +81,12 @@ public class SyncJob extends SimpleJobService {
}
private void showNotification() {
NotificationService service = new NotificationService(getApplicationContext());
GradeNotify gradeNotify = new GradeNotify(getApplicationContext());
service.notify(service.notificationBuilder()
gradeNotify.notify(gradeNotify.notificationBuilder()
.setContentTitle(getStringTitle())
.setContentText(getStringContent())
.setSmallIcon(R.drawable.ic_stat_notify)
.setSmallIcon(R.drawable.ic_notify_grade)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)

View File

@ -5,6 +5,7 @@ import android.os.Bundle;
import javax.inject.Inject;
import butterknife.ButterKnife;
import io.github.wulkanowy.services.NotificationService;
import io.github.wulkanowy.services.SyncJob;
import io.github.wulkanowy.ui.base.BaseActivity;
import io.github.wulkanowy.ui.login.LoginActivity;
@ -47,4 +48,9 @@ public class SplashActivity extends BaseActivity implements SplashContract.View
public void startSyncService(int interval, boolean useOnlyWifi) {
SyncJob.start(getApplicationContext(), interval, useOnlyWifi);
}
@Override
public void cancelNotifications() {
new NotificationService(getApplicationContext()).cancelAll();
}
}

View File

@ -13,6 +13,8 @@ public interface SplashContract {
void openMainActivity();
void startSyncService(int interval, boolean useOnlyWifi);
void cancelNotifications();
}
@PerActivity

View File

@ -18,6 +18,8 @@ public class SplashPresenter extends BasePresenter<SplashContract.View>
@Override
public void onStart(@NonNull SplashContract.View activity) {
super.onStart(activity);
getView().cancelNotifications();
if (getRepository().isServicesEnable()) {
getView().startSyncService(getRepository().getServicesInterval(),
getRepository().isMobileDisable());

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 730 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 469 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 624 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -122,4 +122,6 @@
<string name="pref_services_switch">Włącz odświeżanie danych w tle</string>
<string name="pref_services_interval">Interwał między odświeżaniem danych</string>
<string name="pref_services_mobile_data">Synchronizacja tylko przez WiFi</string>
<string name="notify_grade_channel">Nowe oceny</string>
</resources>

View File

@ -118,4 +118,6 @@
<string name="pref_services_switch">Enable background data refreshing</string>
<string name="pref_services_interval">Interval between data refreshing</string>
<string name="pref_services_mobile_data">Synchronization via WiFi only</string>
<string name="notify_grade_channel">New grades</string>
</resources>