Show notifications on Screen in Android - java

i am using this code for sending local notifications
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)value, mBuilder.build());
However they display on status bar if phone screen is lock by sliding. I want to show the push notifications on top of screen like whatsapp, facebook, sms messages etc.
How can I make this possible?

You can do this way:
By adding below line:
setPriority(Notification.PRIORITY_MAX)
Full code:
mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int)value, mBuilder.build());
Hope this will help you.

Related

Notification LED not turning on

I tried almost every fix posted here but this code won't turn on Notification LED at all.
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.defaultChannelId);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this,channelId)
.setLights(Color.RED,200,1000)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.drawable.omega_icon)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
Notification notification = notificationBuilder.build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.setLightColor(0xFFFFFFFF);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(1, notification);
}
I'm sending notification from Firebase and everything works perfectly fine except the LED lights. LED notification from other apps work fine.
You can't change channel color notification after Oreo/API 26.
developer.android.com

Firebase my notification not show me the recived time hour

when I receive the notifications, it comes without time, never showing me hour, find the below attached code.
This is my notification code
public void showBigNotification(String title, String message, String url, Intent intent) {
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mCtx,
ID_BIG_NOTIFICATION,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(getBitmapFromURL(url));
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);
Notification notification;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification = mBuilder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.mipmap.ic_launcher))
.setContentText(message)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(ID_BIG_NOTIFICATION, notification);
}
Replace 0 with System.currentTimeMillis() in .setWhen() function.
.setWhen(System.currentTimeMillis())

startForeground not showing notification again

In my foreground service the code for starting service is as follows:
notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setContentIntent(contentIntent)
.setSmallIcon(R.mipmap.ic_launcher) // the status icon
.setWhen(System.currentTimeMillis()) // the time stamp
.setCustomContentView(getSmallContentView())
.setCustomBigContentView(getBigContentView())
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setOngoing(true);
if (Build.VERSION_CODES.O <= Build.VERSION.SDK_INT) {
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelId, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
}
Notification notification = notificationBuilder.build();
// Send the notification.
startForeground(notificationId, notification);
Calling stopForeground(true) and after that calling startForeground(id,notification) does not show notification again in foreground service.
Any help will be appreciated.

How can i open Activity when notification click

I need use notification with click event, i have notification method but this method don't open my activity.
My code:
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
is this possible,
thanks.
Yes, it is possible.
Change Your method, like that;
private void sendNotification(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(getNotificationIcon(), "Action Button", pIntent)
.setContentIntent(pIntent)
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and add your mainactivity
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this code working.
Hey #John it's very simple
you just have to do
Intent intent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent pendingIntent = TaskStackBuilder.create(getApp())
.addNextIntent(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
And set pending Intent in mBuilder
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
.setContentIntent(pendingIntent)
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and you done :)

Notifications Code in Android

I am trying to write a simple notification code in a button that create a notification when i press on it but the notification doesn't appear when i press on the button here is the code i am using
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(SessionsActivity.this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(SessionsActivity.this)
.setContentTitle("hii")
.setContentText("hii2")
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =NotificationManagerCompat.from(SessionsActivity.this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
Intent viewIntent = new Intent(SessionsActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, viewIntent , PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
notificationBuilder.setStyle(inboxStyle);
inboxStyle.setBigContentTitle("sdjshfjnds");
inboxStyle.addLine("sdjjsdfn");
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int NOTIFICATION_ID = 100;
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());

Categories