I am trying to get a notification to be pushed when a tickbox is checked and a button is clicked to update the record. However when this is clicked, it crashes any emulator and phone devices that is running on a version below 24.
I have got the notification to work on 24 or above which shows the implementation for the notificationcompat builder is correct but it just doesn't seem to work on any device version lower than 24.
Notification Builder:
public void showNotification(String tvSeriesName) {
String notificationText = "You watched '" + tvSeriesName +
"'; how about telling others what you thought of it!";
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Watched TV series")
.setContentText("You've watched:" + tvSeriesName)
.setSmallIcon(R.drawable.ic_tv_24dp)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationText))
.setAutoCancel(true)
.setSubText("")
.setNumber(150)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
mNotificationManager.notify(111, notification);
I am expecting the result to be a notification to pop up displaying You watched tvSeriesName as the title and then how about telling others what you thought but as I say the notification just crashes on any device version lower than 24.
your are using code to execute Notification on api 24 and above using channels , you should use this code for api 23 and below :
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
//if you have custom view
RemoteViews contentView = new RemoteViews(getPackageName(),
mBuilder.setSmallIcon(R.drawable.ic_accessibility_white_36dp);
// if you have custom view
mBuilder.setContent(contentView);
mBuilder.setAutoCancel(true);
mBuilder.setContentIntent(pendingIntent);
// do not forget to mention the other info , title , text ,.....
// unique id for NOTIFICATION_ID
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Related
I'm trying to show a notification to the user when a service that runs in the background receives an FCM parcel. The FCM parcel is properly received and parsed, however:
The notification I get on my phone only shows up as a default importance (https://notifee.app/react-native/docs/android/appearance#default) notification.
Here's my code:
// create notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "default")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_HIGH));
NotificationManagerCompat.from(this).notify(0, builder.build());
I have an notification that works fine yesterday, the notification not appears and I don't remember I had touched the code..
that notification must be appears when I get the desired value and must open a pop up dialog we user touch it.
can anyone help please?
the notification code - in side service-:
Intent notifyIntent = new Intent(context.getApplicationContext(), PopUp.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//how I tried to pass data
notifyIntent.putExtra("text1", text1);
notifyIntent.putExtra("text2", text2);
PendingIntent pIntent = PendingIntent.getActivity(context, 1, notifyIntent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new NotificationCompat.Builder(context,CHANNEL_ID2)
.setContentTitle("check this")
.setSmallIcon(R.drawable.ic_delete)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
I'm using android studio emulator API 30
in the same service class I have a Notification and it is appearing to indicate that the service is working in the background, except the Notification that I but its code above it is not working suddenly
I don't know what is the problem, please help
When Issuing the notification with notify the notificationId must be a unique int for each notification. Update your code to include a random unique notificationId
see below
//you can use the timestamp
int notificationId = Integer.parseInt(String.valueOf(System.currentTimeMillis()));
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, n);
Android Create a Notification
I am trying to create a notification for Incoming call. For that I have added two actions in notification. My action text only is displayed.Action icon is not displayed in notification.
I want to add icon near Answer and cancel which I added as AddAction in Notification. I have added action icon like below,
NotificationCompat.Action answerAction = new NotificationCompat.Action.Builder(R.drawable.answer_call_icon, "Answer", pendingIntent).build();
NotificationCompat.Action cancelAction = new NotificationCompat.Action.Builder(R.drawable.cancel, "Cancel", pendingIntent).build();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setLargeIcon((BitmapFactory.decodeResource(getResources(), R.drawable.call_logo)))
.setContentTitle(intent.getStringExtra("Number"))
.setSmallIcon(R.drawable.call_logo)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setFullScreenIntent(pendingIntent, true)
.setCategory(NotificationCompat.CATEGORY_CALL)
.addAction(answerAction)
.addAction(cancelAction)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat nManager = NotificationManagerCompat.from(this);
nManager.notify(2,builder.build());
One more query,
Below is my notification channnel,
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.FLAG_FOREGROUND_SERVICE);
chan.setImportance(NotificationManager.IMPORTANCE_HIGH);
chan.setSound(defaultRingToneUri,audioAttributes);
chan.enableLights(true);
chan.shouldShowLights();
chan.setVibrationPattern(vibrate);
chan.enableVibration(true);
Context context=getApplicationContext();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
Not able to get ringtone while receiving notification. Is the way Im setting sound is right??
Anybody please help me to solve this...
Posted 2 days ago..but till now not able to find solution.
As per the Notifications in Android N blog post:
Notification actions have also received a redesign and are now in a visually separate bar below the notification.
You’ll note that the icons are not present in the new notifications; instead more room is provided for the labels themselves in the constrained space of the notification shade. However, the notification action icons are still required and continue to be used on older versions of Android and on devices such as Android Wear.
So it is expected that you do not see the icons associated with notification actions.
If you need more flexibility in creating the notification layout, go for the custom one.
Ref: https://developer.android.com/training/notify-user/custom-notification
Use the Drawable left/right option to set an icon with text in the button.
// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
// Apply the layouts to the notification
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build();
As I understand: you have a problem with your notification you can use this method :
private void showSmallNotification(NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constant.NOTIFICATION_ID, notification);
}
or this one :
private void showBigNotification(Bitmap bitmap, NotificationCompat.Builder mBuilder, int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {
NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
bigPictureStyle.setBigContentTitle(title);
bigPictureStyle.setSummaryText(Html.fromHtml(message).toString());
bigPictureStyle.bigPicture(bitmap);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constant.NOTIFICATION_ID_BIG_IMAGE, notification);
}
To play ringtone you can make use of android mediaplayer as shown below:
Var _mediaPlayer = MediaPlayer.Create(this,
Android.Provider.Settings.System.DefaultRingtoneUri);
_mediaPlayer.Start();
Task.Delay(60 * 1000).ContinueWith(t => _mediaPlayer?.Stop()); //stop after 60sec
You can take the global instance of _mediaPlayer and stop this as per user interaction with notification.
This is the c# xamarin android example, you can use the same class with java syntax.
Is it possible to set the duration of the headsup notification to unlimited? right now its displayed just for 5 seconds. Already tried different things like changing the category. But the duration always is 5 seconds.
Here is my code:
Notification notification =
notificationBuilder
.setCategory(Notification.CATEGORY_CALL)
.setContentText("TKSE Werk Duisburg")
.setSmallIcon(R.drawable.ic_tk_individual_signet_logo)
.setOngoing(true)
.setAutoCancel(false)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(contentIntent)
.setCustomHeadsUpContentView(viewNotificationHeadsUp) .setCustomContentView(viewNotificationSmall)
.setPriority(Notification.PRIORITY_MAX)
.setVibrate(new long[20]).build();
Tried the same things like on this thread: Controlling Android Notification Duration for HeadsUp but it did not help me.
Interesting fact:
On my Developer Phone a Samsung S5 mini -> its displayed with no time limit
On another Developer Phone a Samsung S7 -> its displayed 5 seconds
This should work. I have added extra onGoing(true) & category call for notification category.
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// 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);
}
// assuming your main activity
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setCategory(Notification.CATEGORY_CALL)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setFullScreenIntent(pendingIntent,true)
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
PS. import android.app.Notification; for setting notification category (call)
Update Android 10
You need to add priority along with category for on going notification.
val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification.
.setFullScreenIntent(fullScreenPendingIntent, true)
val incomingCallNotification = notificationBuilder.build()
Source
I have found this page and it has worked
https://developer.android.com/training/notify-user/time-sensitive
val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val notificationBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Incoming call")
.setContentText("(919) 555-1234")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
// Use a full-screen intent only for the highest-priority alerts where you
// have an associated activity that you would like to launch after the user
// interacts with the notification. Also, if your app targets Android 10
// or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
// order for the platform to invoke this notification.
.setFullScreenIntent(fullScreenPendingIntent, true)
and of course use with foregroundService
val incomingCallNotification = notificationBuilder.build()
Try that solution (the one that you have posted as the link..) but after doing that change dont forget to change the NOTIFICATION_ID sometimes if the notification id is not changed some of the previous attributes of the notifications stays .. so
Change the notification id when you change any attribute of notification builder
i cant seem to understand how to display multiple notifications without one overlaying another. In my case it only displays one at the time.
picture 1
My goal is to get it to work like on the screenshot below
picture 2
What should I change or maybe add to my code?
chunk of code assigned for notifications
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
Intent intent = new Intent(ctx, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("com.example.romanchuk.appisode.notifyId", id);
intent.putExtra("com.example.romanchuk.appisode.show_id", show_id);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, sNotificationId /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(message);
bigText.setBigContentTitle(getString(R.string.app_name));
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
Notification notification = null;
notification = builder.setSmallIcon(R.mipmap.ic_launcher).setTicker(title).setWhen(0)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(getResources().getColor(R.color.color_accent))
.setContentTitle("Appisode")
.setContentIntent(pendingIntent)
.setFullScreenIntent(pendingIntent, true)
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setStyle(inboxStyle)
.setSmallIcon(R.drawable.small_icon)
.setSound(defaultSoundUri).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(sNotificationId++, notification);
}
If you want to show multiple notifications, You Notification id should be different, If the notification id already exists in the notifications it will override that notification.
notificationManager.notify(sNotificationId++, notification);
In this sNotificationId should be different for all notification
If your id or show_id is int and not constant and if it will be different for each notification you can use that also as notification id.
Or try to give different tag for each notification like this,
notificationManager.notify(String.valueOf(System.currentTimeMillis()), sNotificationId++, notification);