这是我的解决方案:
public class AlarmReceiver extends BroadcastReceiver {
public static String NOTIFICATION_CHANNEL_ID = "notification-id";
public static String NOTIFICATION = "Notification";
public static String DESCRIPTION = "Channel description";
@TargetApi(26)
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("RECEIVE", "received2");
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pi = PendingIntent.getActivity(context, intent.getIntExtra("NotifID", 1), new Intent(context, CalendarActivity.class),PendingIntent.FLAG_CANCEL_CURRENT);
// get current locale
String locale; // for getting locale
locale = Locale.getDefault().getLanguage();
if(locale.equalsIgnoreCase("sk")) {
NOTIFICATION = "Notifikácia";
DESCRIPTION = "Popis";
}
if (VERSION.SDK_INT >= 26) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(DESCRIPTION);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.setLockscreenVisibility(Notification.DEFAULT_SOUND);
notificationChannel.setVibrationPattern(new long[]{1000, 500, 500, 200, 200, 200});
notificationChannel.enableVibration(true);
manager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle(intent.getStringExtra("AppName"))
.setContentText(intent.getStringExtra("lname"))
.setSmallIcon(R.drawable.ic_launcher)
;
//manager.notify(1,builder.build());
manager.notify(intent.getIntExtra("NotifID", 1), builder.build());
}
}