mirror of
https://github.com/jfoucry/Pilldroid.git
synced 2024-11-13 00:51:35 +01:00
New NotificationPublisher for android >= 28;
Optimize import.
This commit is contained in:
parent
b2287c99d4
commit
0afb8bf34e
1 changed files with 55 additions and 15 deletions
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue