Created
August 29, 2017 07:49
-
-
Save sergeykomlach/2ff943b86d2f93e92dc3c9298a93e272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final String GROUP_ID = "my_group"; | |
final String CHANNEL_ID = "my_channel"; | |
//called in Application.onCreate() | |
private void init(){ | |
if(Build.VERSION.SDK_INT >= 26) | |
{ | |
NotificationManager notificationManager = getSystemService(NotificationManager.class); | |
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_ID, getResources().getString(R.string.app_name))); | |
NotificationChannel notificationChannel = | |
new NotificationChannel(CHANNEL_ID, getResources().getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW); | |
notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_SECRET); | |
notificationChannel.setShowBadge(true); | |
notificationChannel.setGroup(GROUP_ID); | |
notificationManager.createNotificationChannel(notificationChannel); | |
} | |
} | |
//on API<=25 notification doesnt shown in Statusbar, but visible in notifications list | |
private void secretNotification(){ | |
NotificationCompat.Builder notif = new NotificationCompat.Builder( | |
this, CHANNEL_ID) //doesnt work on API26 without ChannelID | |
.setContentTitle( | |
this.getResources().getString(R.string.app_name)) | |
.setContentText(text) | |
.setChannelId(CHANNEL_ID) | |
.setGroup(GROUP_ID) | |
.setAutoCancel(false) | |
.setPriority(NotificationCompat.PRIORITY_MIN) | |
.setVisibility(NotificationCompat.VISIBILITY_SECRET) | |
.setSmallIcon(R.drawable.icon); | |
startForeground(NOTIFICATION_ID, notif.build()); | |
} | |
//notification visible always | |
private void visibleNotification(){ | |
NotificationManagerCompat nMgr = | |
NotificationManagerCompat.from(this); | |
NotificationCompat.Builder notif = new NotificationCompat.Builder(this, CHANNEL_ID)//doesnt work on API26 without ChannelID | |
.setContentTitle(this.getResources().getString(R.string.title)) | |
.setChannelId(CHANNEL_ID) | |
.setGroup(GROUP_ID) | |
.setContentText(this.getResources().getString(R.string.text)) | |
.setSmallIcon(android.R.drawable.ic_dialog_alert); | |
nMgr.notify(NOTIFICATION_ID2, notif.build()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment