From 0afb8bf34eab1a84394974a2ebafc898e0b3efdf Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 19 Jul 2020 20:02:53 +0200 Subject: [PATCH] New NotificationPublisher for android >= 28; Optimize import. --- .../pilldroid/NotificationPublisher.java | 70 +++++++++++++++---- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java index 55a832b..a42974a 100644 --- a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -1,35 +1,75 @@ package net.foucry.pilldroid; -import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.VibrationEffect; -import android.os.Vibrator; import android.util.Log; +import androidx.core.app.NotificationCompat; + /** * Created by jfoucry on 6/23/16. + * Publish notification */ + + public class NotificationPublisher extends BroadcastReceiver { private static String TAG = NotificationPublisher.class.getName(); public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; + /** + * onReceive notification + * @param Context context + * @param Intent intent + */ public void onReceive(Context context, Intent intent) { - Log.i(TAG, "Receive notification"); + Log.d(TAG, "Receive notification"); - NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - - Notification notification = intent.getParcelableExtra(NOTIFICATION); - int id = intent.getIntExtra(NOTIFICATION_ID,0); - if (notificationManager != null) { - notificationManager.notify(id, notification); - } - Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - assert vibrator != null; - vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); + String channel_id = createNotificationChannel(context); + NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel_id) + .setSmallIcon(R.drawable.ic_pill) + .setContentTitle(context.getString(R.string.app_name)) + .setContentText(context.getString(R.string.pharmacy)) + .setStyle(new NotificationCompat.BigTextStyle() + .bigText(context.getString(R.string.pharmacy))) + .setPriority(NotificationCompat.PRIORITY_DEFAULT); } -} + + /** + * createNotificationChannelid for android API >= 28 + * @param Context context + * @return String channel_id + */ + public static String createNotificationChannel(Context context) { + + // The id of the channel. + String channelId = "Channel_id"; + + // The user-visible name of the channel. + CharSequence channelName = context.getString(R.string.app_name); + // The user-visible description of the channel. + String channelDescription = "Pilldroid Alert"; + int channelImportance = NotificationManager.IMPORTANCE_DEFAULT; + // int channelLockscreenVisibility = Notification.; + + // Initializes NotificationChannel. + NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, + channelImportance); + notificationChannel.setDescription(channelDescription); + notificationChannel.enableVibration(true); + // notificationChannel.setLockscreenVisibility(channelLockscreenVisibility); + + // Adds NotificationChannel to system. Attempting to create an existing notification + // channel with its original values performs no operation, so it's safe to perform the + // below sequence. + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + assert notificationManager != null; + notificationManager.createNotificationChannel(notificationChannel); + + return channelId; + } +} \ No newline at end of file