Skip to content

Instantly share code, notes, and snippets.

@Redman1037
Created March 14, 2018 10:32
Show Gist options
  • Save Redman1037/6d7fa901c7c12ee3d1d590579c9cff78 to your computer and use it in GitHub Desktop.
Save Redman1037/6d7fa901c7c12ee3d1d590579c9cff78 to your computer and use it in GitHub Desktop.
Notifications Android
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(Constants.IS_FROM_NOTIFICATION, true);
sendBroadcast(new Intent(Constants.REFRESH_STATUS));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel;
// if (type.equals("1") || type.equals("2"))
notificationChannel = new NotificationChannel("TripId", "Trip", importance);
// else
// notificationChannel = new NotificationChannel("ChatID", "Chat", importance);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
builder.setPriority(NotificationManager.IMPORTANCE_HIGH);
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
builder.setPriority(Notification.PRIORITY_MAX);
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder = builder
.setSmallIcon(R.mipmap.ic_notification_icon)
// .setColor(ContextCompat.getColor(context, R.color.color))
.setContentTitle(title)
.setTicker(getString(R.string.app_name))
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
notificationManager.notify((int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE), builder.build());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment