Skip to content

Instantly share code, notes, and snippets.

@choiks14
Last active October 18, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choiks14/24106a74c5f39ce6c25d778537a2d157 to your computer and use it in GitHub Desktop.
Save choiks14/24106a74c5f39ce6c25d778537a2d157 to your computer and use it in GitHub Desktop.
private void showNotification(String title, String message, Intent intent) {
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), Defines.REQUEST_GOTO_HOME, intent, PendingIntent.FLAG_ONE_SHOT);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setShowWhen(true);
builder.setContentTitle(title);
builder.setContentText(message);
builder.setTicker(message);
builder.setWhen(System.currentTimeMillis());
builder.setContentIntent(pendingIntent);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= 21) {
builder.setColor(getApplication().getResources().getColor(R.color.colorAccent))
.setVisibility(Notification.VISIBILITY_PUBLIC);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(MyApplication.CHANNEL_ID, MyApplication.CHANNEL_ID, NotificationManager.IMPORTANCE_MAX);
notificationChannel.setDescription("channel description");
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 100, 200});
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
builder.setChannelId(notificationChannel.getId());
}
Notification notification = builder.build();
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Defines.NOTI_PUSH, notification);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment