Skip to content

Commit

Permalink
Add notification channel. Fix #79
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersantos committed Feb 17, 2018
1 parent 78e9fbd commit 4914443
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.javiersantos.appupdater;

import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
Expand Down Expand Up @@ -72,28 +73,32 @@ static Snackbar showUpdateNotAvailableSnackbar(final Context context, String con
}

static void showUpdateAvailableNotification(Context context, String title, String content, UpdateFrom updateFrom, URL apk, int smallIconResourceId) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
initNotificationChannel(context, notificationManager);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, context.getPackageManager().getLaunchIntentForPackage(UtilsLibrary.getAppPackageName(context)), PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntentUpdate = PendingIntent.getActivity(context, 0, UtilsLibrary.intentToUpdate(context, updateFrom, apk), PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(content)
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setSmallIcon(smallIconResourceId)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setOnlyAlertOnce(true)
.setAutoCancel(true)
NotificationCompat.Builder builder = getBaseNotification(context, contentIntent, title, content, smallIconResourceId)
.addAction(R.drawable.ic_system_update_white_24dp, context.getResources().getString(R.string.appupdater_btn_update), pendingIntentUpdate);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}

static void showUpdateNotAvailableNotification(Context context, String title, String content, int smallIconResourceId) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
initNotificationChannel(context, notificationManager);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, context.getPackageManager().getLaunchIntentForPackage(UtilsLibrary.getAppPackageName(context)), PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
NotificationCompat.Builder builder = getBaseNotification(context, contentIntent, title, content, smallIconResourceId)
.setAutoCancel(true);

notificationManager.notify(0, builder.build());
}

private static NotificationCompat.Builder getBaseNotification(Context context, PendingIntent contentIntent, String title, String content, int smallIconResourceId) {
return new NotificationCompat.Builder(context, context.getString(R.string.appupdater_channel))
.setContentIntent(contentIntent)
.setContentTitle(title)
.setContentText(content)
Expand All @@ -103,8 +108,16 @@ static void showUpdateNotAvailableNotification(Context context, String title, St
.setOnlyAlertOnce(true)
.setAutoCancel(true);

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}

private static void initNotificationChannel(Context context, NotificationManager notificationManager) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
context.getString(R.string.appupdater_channel),
context.getString(R.string.appupdater_channel_name),
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(mChannel);
}
}

}
1 change: 1 addition & 0 deletions library/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<string name="appupdater_btn_update">Actualizar</string>
<string name="appupdater_btn_disable">No mostrar de nuevo</string>
<string name="appupdater_btn_dismiss">Ocultar</string>
<string name="appupdater_channel_name">Notificaciones de actualización</string>
</resources>
3 changes: 3 additions & 0 deletions library/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
<string name="appupdater_btn_update">Update</string>
<string name="appupdater_btn_disable">Don\'t show again</string>
<string name="appupdater_btn_dismiss">Dismiss</string>
<string name="appupdater_channel_name">Update notifications</string>

<string name="appupdater_channel" translatable="false">appupdater_channel_01</string>
</resources>

0 comments on commit 4914443

Please sign in to comment.