I need the code of Android notification with timing when notification was received.
I was testing notification in my app with following code
public void onPageFinished(WebView view, String url) {
Log.d("App Loaded ==", "Do");
NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify=new Notification.Builder
(getApplicationContext()).setContentTitle("testing").setContentText("This is testing").
setContentTitle("time notification").setSmallIcon(R.drawable.ic_notification).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
Kindly suggest why this is not working on page finished event. No notification is being triggered.
You have to check android version from where you are testing the app.
if its Oreo+ then you have to add notification channel, otherwise you will not get notification.
and please replace the Notification class with NotificationCompat as its deprecated.
try with this code you will surely get notification.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(artist)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCustomContentView(contentView)
.setColor(ctx.getResources().getColor(R.color.colorPrimary))
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);
if (notificationManager != null) {
notification =builder.build();
notificationManager.notify(1, builder.build());
}
and if you want to log time while notification coming, you can add 1 line of code inside the Onreceived : Log.d(TAG, "Time: "+System.currentTimeMillis());
Related
I have implement Push notification firebase services class and add on my project and send notification API Request through so notification proper receive but notification sound work to app active condition only but it will be require all notification receive time.
any one this type condition face and resolve please suggest changes.
String title = Objects.requireNonNull(remoteMessage.getNotification()).getTitle();
String text = remoteMessage.getNotification().getBody();
remoteMessage.getMessageType();
// String channelId = remoteMessage.getNotification().getChannelId();
// Log.d("channelId", "onMessageReceived: "+channelId);
final String CHANNEL_ID = "HEADS_UP_NOTIFICATION";
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID,
"Heads Up notification",
NotificationManager.IMPORTANCE_HIGH
);
getSystemService(NotificationManager.class).createNotificationChannel(channel);
Notification.Builder notification = new Notification.Builder(this,CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(new long[] { 0,100,300,500,800,1000, 1000, 1000, 1000, 1000 })
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_SOUND)
.setVisibility(Notification.VISIBILITY_PUBLIC);
// custom notification
/* MediaPlayer mp;
mp = MediaPlayer.create(PushNotificationServices.this, R.raw.short_notification_sound);
mp.start();*/
#SuppressLint("UnspecifiedImmutableFlag") PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
notification.setContentIntent(contentIntent);
NotificationManagerCompat.from(this).notify(1,notification.build());
super.onMessageReceived(remoteMessage);
Try to use setSound method instead of setDefaults.
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification: Notification.Builder = Builder(this, CHANNEL_ID)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.demo)
.setAutoCancel(true)
.setVibrate(longArrayOf(0, 100, 300, 500, 800, 1000, 1000, 1000, 1000, 1000))
.setOngoing(true)
.setPriority(Notification.PRIORITY_MAX)
--> .setSound(defaultSoundUri)
.setVisibility(Notification.VISIBILITY_PUBLIC)
First You have uninstall your app then try again.
If this does not work for then try using below code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = CommonUtill.random();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody).setBigContentTitle(getString(R.string.app_name)))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
NotificationChannel mChannel = new NotificationChannel(channelId,
getString(R.string.app_name),
NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
mChannel.setDescription("desc");
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setSound(sound, attributes); // This is IMPORTANT
if (notificationManager != null)
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(943/* ID of notification */, notificationBuilder.build());
This is reference link
Android Push Notification Sound is missing (Not Working ) background and foreground on android
I literally have tried everything seriously every method and every snippet but still, I was not able to show heads-up notifications on Chinese brand devices.
so yesterday I thought why not try it again but after all again still I'm not able to show heads-up notification until I manually goto the app into the settings and give floating permission for the app.
Now most of you may say why not to navigate the user to the setting when he/she first opens up the app but nobody likes that even there are other apps (I'm not talking about white list app like WhatsApp) which have 10K downloads are able to show heads up notification
Here is my code, btw I have tried setting the sound, vibration, and light but still heads up are not showing, and yes I do uninstall my app after every build
public void showNotification(View v){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.ic_launcher_background);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
// .setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.ic_launcher_background);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
}
I have two solutions. Try them out and see if any works.
Yours actually works Like this
public void showNotification(){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel nc = new NotificationChannel("n","pop up notification", NotificationManager.IMPORTANCE_HIGH);
nc.enableLights(true);
nc.setLightColor(Color.BLUE);
nc.enableVibration(true);
nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager nm = getSystemService(NotificationManager.class);
nm.createNotificationChannel(nc);
}
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(this,"n")
.setContentTitle("Pop up notification")
.setSmallIcon(R.drawable.alex);
}else{
notification = new Notification.Builder(this)
.setContentTitle("Pop up notification")
//.setPriority(Notification.PRIORITY_MAX)
.setSmallIcon(R.drawable.alex);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification.build());
Mine notifyUser("Order Delivery", "Your order has arrived"):
public void notifyUser(String title, String text){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify_001");
Intent intent = new Intent(this, ActivityToOpenWhenNotificationIsTapped.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(text);
bigTextStyle.setSummaryText("Big summary text. Comment if you want.");
builder.setAutoCancel(true);
builder.setContentIntent(activity);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setPriority(1);
builder.setSmallIcon(R.drawable.alex);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.alex));
builder.setStyle(bigTextStyle);
builder.setAutoCancel(true);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setGroup("Pixels");
Notification build = builder.build();
build.flags |= 16;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
String str3 = "Pixels_V1.0";
notificationManager.createNotificationChannel(new NotificationChannel(str3, "Pixels Notification", NotificationManager.IMPORTANCE_DEFAULT));
builder.setChannelId(str3);
}
int NOTIFICATION_ID = new Random(System.currentTimeMillis()).nextInt(1000);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}
I'm programming some Android App that must make some Notification/Alarm after event occurs. Im wondering is there any function for NotificationManager like requireInteraction()?
Now when the certain event occurs the app just shows one notification for 1 sec, that's it..i'd like user to click OK to stop this vibration/sound
I found some code for notification from here:
NotificationCompat.Builder deprecated in Android O
Thanks #Mehul
public void showNotification (String from, String notification,
Intent intent) {
int requestID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
requestID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/requestID, mNotification);
}
This one shows notification and it doesnt wait for user Input
If there is a need to add the buttons or action after clicking on notification you can use in your builder:
To add an button:
.addAction(new NotificationCompat.Action(*icon*, "Title", *intent for notification*));
or to add action that happen after user click the notification
.setContentIntent(*intent*);
Check the documentation about tab action and actions if you need more details.
Currently developing an android application, and my notifications are not shown in the phone.
I am connecting my phisical phone to android studio, and I see that the method that send notification is been called, but is not displaying any notifications in the phone.
private void createNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_chat)
.setContentTitle("New match!!!")
.setContentText("You got a new match!!!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mNotificationManager.notify(001,builder.build());
}
previously im initializing mNotoficationManager as follows:
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
any idea on why my phone doesnt display my notification when I get that method call? is been called from a service that I created to change events in the database.
Found this code that works from android 8 onwards.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "Tortuga");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("New match on WeRoom")
.setContentText("You got a new match, you can now chat with someone more!!!")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String NOTIFICATION_CHANNEL_ID = "1001";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
I'am using FCM for getting new notification which I am able to show without any problem but when i open the app all my notification disappeared
this my code for creating new notification
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext,mContext.getString(R.string.default_notification_channel_id));
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mContext.getString(R.string.default_notification_channel_id), name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
when click on a notification it opens an activity with extra data in
my intent
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", "wwwwwwww");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
now when i click on any notification it opens MainActivity with some extra value on which Main activity depends.
But when i click on a notification and it launches the activity all other notification disappeared.So, how can i prevent that and hide only clicked notification so user can see other notification
not quite sure whether it'll work or not but can u please try with setting the request code unique rather than 0.
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
replace 0 with the following
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
try this
**.setPriority(NotificationManager.IMPORTANCE_HIGH)**
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(ICON)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notification);