Notifications improvements (#78)
* Refactor notifications * Change grade notify icon
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -3,29 +3,22 @@ package io.github.wulkanowy.services;
|
|||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationChannel;
|
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
import android.support.annotation.StringRes;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
class NotificationService {
|
public class NotificationService {
|
||||||
|
|
||||||
private static final String CHANNEL_ID = "Wulkanowy_New_Grade_Channel";
|
|
||||||
|
|
||||||
private static final String CHANNEL_NAME = "New Grade Channel";
|
|
||||||
|
|
||||||
private NotificationManager manager;
|
private NotificationManager manager;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
NotificationService(Context context) {
|
public NotificationService(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
createChannel();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify(Notification notification) {
|
void notify(Notification notification) {
|
||||||
@ -33,23 +26,32 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NotificationCompat.Builder notificationBuilder() {
|
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)
|
public void cancelAll() {
|
||||||
private void createChannel() {
|
getManager().cancelAll();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NotificationManager getManager() {
|
NotificationManager getManager() {
|
||||||
if (manager == null) {
|
if (manager == null) {
|
||||||
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
return manager;
|
return manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String getString(@StringRes int stringId) {
|
||||||
|
return context.getString(stringId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(26)
|
||||||
|
void createChannel() {
|
||||||
|
}
|
||||||
|
|
||||||
|
String getChannelId() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
@ -81,12 +81,12 @@ public class SyncJob extends SimpleJobService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void showNotification() {
|
private void showNotification() {
|
||||||
NotificationService service = new NotificationService(getApplicationContext());
|
GradeNotify gradeNotify = new GradeNotify(getApplicationContext());
|
||||||
|
|
||||||
service.notify(service.notificationBuilder()
|
gradeNotify.notify(gradeNotify.notificationBuilder()
|
||||||
.setContentTitle(getStringTitle())
|
.setContentTitle(getStringTitle())
|
||||||
.setContentText(getStringContent())
|
.setContentText(getStringContent())
|
||||||
.setSmallIcon(R.drawable.ic_stat_notify)
|
.setSmallIcon(R.drawable.ic_notify_grade)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
.setDefaults(NotificationCompat.DEFAULT_ALL)
|
.setDefaults(NotificationCompat.DEFAULT_ALL)
|
||||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
|
@ -5,6 +5,7 @@ import android.os.Bundle;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import butterknife.ButterKnife;
|
import butterknife.ButterKnife;
|
||||||
|
import io.github.wulkanowy.services.NotificationService;
|
||||||
import io.github.wulkanowy.services.SyncJob;
|
import io.github.wulkanowy.services.SyncJob;
|
||||||
import io.github.wulkanowy.ui.base.BaseActivity;
|
import io.github.wulkanowy.ui.base.BaseActivity;
|
||||||
import io.github.wulkanowy.ui.login.LoginActivity;
|
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) {
|
public void startSyncService(int interval, boolean useOnlyWifi) {
|
||||||
SyncJob.start(getApplicationContext(), interval, useOnlyWifi);
|
SyncJob.start(getApplicationContext(), interval, useOnlyWifi);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelNotifications() {
|
||||||
|
new NotificationService(getApplicationContext()).cancelAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,8 @@ public interface SplashContract {
|
|||||||
void openMainActivity();
|
void openMainActivity();
|
||||||
|
|
||||||
void startSyncService(int interval, boolean useOnlyWifi);
|
void startSyncService(int interval, boolean useOnlyWifi);
|
||||||
|
|
||||||
|
void cancelNotifications();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PerActivity
|
@PerActivity
|
||||||
|
@ -18,6 +18,8 @@ public class SplashPresenter extends BasePresenter<SplashContract.View>
|
|||||||
@Override
|
@Override
|
||||||
public void onStart(@NonNull SplashContract.View activity) {
|
public void onStart(@NonNull SplashContract.View activity) {
|
||||||
super.onStart(activity);
|
super.onStart(activity);
|
||||||
|
getView().cancelNotifications();
|
||||||
|
|
||||||
if (getRepository().isServicesEnable()) {
|
if (getRepository().isServicesEnable()) {
|
||||||
getView().startSyncService(getRepository().getServicesInterval(),
|
getView().startSyncService(getRepository().getServicesInterval(),
|
||||||
getRepository().isMobileDisable());
|
getRepository().isMobileDisable());
|
||||||
|
BIN
app/src/main/res/drawable-hdpi/ic_notify_grade.png
Normal file
After Width: | Height: | Size: 393 B |
Before Width: | Height: | Size: 1.1 KiB |
BIN
app/src/main/res/drawable-mdpi/ic_notify_grade.png
Normal file
After Width: | Height: | Size: 299 B |
Before Width: | Height: | Size: 730 B |
BIN
app/src/main/res/drawable-xhdpi/ic_notify_grade.png
Normal file
After Width: | Height: | Size: 469 B |
Before Width: | Height: | Size: 1.6 KiB |
BIN
app/src/main/res/drawable-xxhdpi/ic_notify_grade.png
Normal file
After Width: | Height: | Size: 624 B |
Before Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 3.3 KiB |
@ -122,4 +122,6 @@
|
|||||||
<string name="pref_services_switch">Włącz odświeżanie danych w tle</string>
|
<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_interval">Interwał między odświeżaniem danych</string>
|
||||||
<string name="pref_services_mobile_data">Synchronizacja tylko przez WiFi</string>
|
<string name="pref_services_mobile_data">Synchronizacja tylko przez WiFi</string>
|
||||||
|
|
||||||
|
<string name="notify_grade_channel">Nowe oceny</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -118,4 +118,6 @@
|
|||||||
<string name="pref_services_switch">Enable background data refreshing</string>
|
<string name="pref_services_switch">Enable background data refreshing</string>
|
||||||
<string name="pref_services_interval">Interval between 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="pref_services_mobile_data">Synchronization via WiFi only</string>
|
||||||
|
|
||||||
|
<string name="notify_grade_channel">New grades</string>
|
||||||
</resources>
|
</resources>
|
||||||
|